package com.huiju.estateagents.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.entity.TaOrg;
import com.huiju.estateagents.service.ITaOrgService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 公司表  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-09-11
 */
@RestController
@RequestMapping("/")
public class TaOrgController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(TaOrgController.class);

    @Autowired
    public ITaOrgService iTaOrgService;


    /**
     * 分页查询列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/center/taOrg", method = RequestMethod.GET)
    public ResponseBean taOrgList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
            IPage<TaOrg> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaOrg> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("create_date");

            IPage<TaOrg> result = iTaOrgService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taOrgList -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     *
     * @param taOrg 实体对象
     * @return
     */
    @RequestMapping(value = "/center/taOrg", method = RequestMethod.POST)
    public ResponseBean taOrgAdd(@RequestBody TaOrg taOrg) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaOrgService.save(taOrg)) {
                responseBean.addSuccess(taOrg);
            } else {
                responseBean.addError("fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taOrgAdd -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     */
    @ResponseBody
    @RequestMapping(value = "/taOrg/{id}", method = RequestMethod.DELETE)
    public ResponseBean taOrgDelete(@PathVariable Integer id) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaOrgService.removeById(id)) {
                responseBean.addSuccess("success");
            } else {
                responseBean.addError("fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taOrgDelete -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     *
     * @param id    实体ID
     * @param taOrg 实体对象
     * @return
     */
    @RequestMapping(value = "/taOrg/{id}", method = RequestMethod.PUT)
    public ResponseBean taOrgUpdate(@PathVariable Integer id,
                                    @RequestBody TaOrg taOrg) {
        ResponseBean responseBean = new ResponseBean();
        try {
            taOrg.setOrgId(id);
            if (iTaOrgService.updateById(taOrg)) {
                responseBean.addSuccess(taOrg);
            } else {
                responseBean.addError("fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taOrgUpdate -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     *
     * @param id 实体ID
     */
    @RequestMapping(value = "/center/taOrg/{id}", method = RequestMethod.GET)
    public ResponseBean taOrgGet(@PathVariable Integer id) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaOrgService.getById(id));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taOrgDelete -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 条件查询组织列表
     *
     * @param pageNum
     * @param pageSize
     * @param orgName
     * @param miniAppName
     * @return
     */
    @RequestMapping(value = "/api/channel/contract/listOrg", method = RequestMethod.GET)
    public ResponseBean listOrgForContract(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                           String orgName, String miniAppName) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean = iTaOrgService.listOrgForContract(pageNum, pageSize, orgName, miniAppName);
            logger.info("TaOrgController.listOrgForContract 返回结果：", JSONObject.toJSONString(responseBean));
        } catch (Exception e) {
            logger.error("TaOrgController.listOrgForContract 异常:", e);
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
