package com.huiju.estateagents.eContract.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.eContract.entity.TaCompanyOrg;
import com.huiju.estateagents.eContract.service.ITaCompanyOrgService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 * 企业和组织关系表  前端控制器
 * </p>
 *
 * @author fxf
 * @since 2020-03-26
 */
@RestController
@RequestMapping("/api")
public class TaCompanyOrgController extends BaseController {

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

    @Autowired
    public ITaCompanyOrgService iTaCompanyOrgService;


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

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

    /**
     * 保存对象
     *
     * @param taCompanyOrg 实体对象
     * @return
     */
    @RequestMapping(value = "/channel/contract/taCompanyOrg", method = RequestMethod.POST)
    public ResponseBean taCompanyOrgAdd(@RequestBody TaCompanyOrg taCompanyOrg) {
        ResponseBean responseBean = new ResponseBean();
        try {

            // 校验参数
            if (taCompanyOrg.getOrgId() == null || taCompanyOrg.getCompanyId() == null) {
                responseBean.addError("参数错误");
                return responseBean;
            }

            // 校验企业是否已经关联组织
            QueryWrapper<TaCompanyOrg> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("org_id",taCompanyOrg.getOrgId());
            queryWrapper.eq("company_id",taCompanyOrg.getCompanyId());
            TaCompanyOrg companyOrg = iTaCompanyOrgService.getOne(queryWrapper);
            if(companyOrg != null){
                responseBean.addError("所选企业已关联了所选组织，不能重复关联");
                return responseBean;
            }

            taCompanyOrg.setCreateDate(LocalDateTime.now());
            if (iTaCompanyOrgService.save(taCompanyOrg)) {
                responseBean.addSuccess(taCompanyOrg);
            } else {
                responseBean.addError("fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taCompanyOrgAdd -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

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

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


    /**
     * 条件查询公司组织列表
     *
     * @param pageNum
     * @param pageSize
     * @param companyName
     * @param companyCode
     * @param certifiedStatus
     * @param orgName
     * @param miniAppName
     * @return
     */
    @RequestMapping(value = "/channel/contract/listCompanyOrg", method = RequestMethod.GET)
    public ResponseBean listCompanyAndOrg(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                          String companyName,
                                          String companyCode,
                                          String certifiedStatus,
                                          Integer companyStatus,
                                          String orgName,
                                          String miniAppName) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean = iTaCompanyOrgService.listCompanyOrg(pageNum, pageSize, companyName, companyCode, certifiedStatus,companyStatus, orgName, miniAppName);
            logger.info("TaCompanyOrgController.listCompanyOrg 返回结果：{}", JSONObject.toJSONString(responseBean));
        } catch (Exception e) {
            logger.error("TaCompanyOrgController.listCompanyOrg 异常：", e);
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
