package com.bonus.hn.bearing.capacity.backstage.corporateOrg.controller;


import com.bonus.hn.bearing.capacity.backstage.corporateOrg.dao.CorporateDao;
import com.bonus.hn.bearing.capacity.backstage.corporateOrg.entity.CorporateBean;
import com.bonus.hn.bearing.capacity.backstage.corporateOrg.service.CorporateService;
import com.bonus.hn.bearing.capacity.backstage.manager.annotation.LogAnnotation;
import com.bonus.hn.bearing.capacity.backstage.manager.entity.MapBean;
import com.bonus.hn.bearing.capacity.backstage.manager.model.SysUser;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.AjaxRes;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.GlobalConst;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.R;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 公司管理表控制层
 * @author jjLV
 * @Data 2023/11/08 9:30
 *
 */
@RestController
@RequestMapping("corporate")
public class CorporateController {
    /**
     * 服务对象
     */
    private static final Logger log = LoggerFactory.getLogger("CareerController");
    @Resource
    private CorporateService service;

/******************************************************公司组织机构公司管理****************************************************************/
    @LogAnnotation
    @PostMapping("getFirmContent")
    @ApiOperation(value = "公司管理-列表")
    public R getFirmList(CorporateBean o){
        List<CorporateBean> list = service.getFirmList(o);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }

    @RequestMapping(value = "addFirmInfo", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "公司管理-新增")
    public AjaxRes addFirmInfo(@RequestBody CorporateBean bean) throws Exception {
        AjaxRes ar = new AjaxRes();
        int cb = service.addFirmInfo(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
        }
        return  ar;
    }

    @RequestMapping(value = "getListFirm", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "公司管理-查询人员信息")
    public AjaxRes getListFirm(String id){
        AjaxRes ar = new AjaxRes();
        CorporateBean corporateBean = service.getListFirm(id);
        Map<String, Object> map = new HashMap<>(100);
        if(corporateBean !=null){
            map.put("corporateBean",corporateBean);
        }
        ar.setSucceed(map,GlobalConst.DATA_SUCCEED);
        return ar;
    }

    @RequestMapping(value = "updateFirm", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "公司管理-修改")
    public AjaxRes updateFirm(@RequestBody CorporateBean bean) throws Exception {
        AjaxRes ar = new AjaxRes();
        int cb = service.updateFirm(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
        }
        return  ar;
    }

    @RequestMapping(value = "delFirmById", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "公司管理-删除")
    public AjaxRes delFirmById(String id) throws Exception {
        AjaxRes ar = new AjaxRes();
        int cb = service.delFirmById(id);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
        }
        return ar;
    }



/******************************************************公司组织机构分公司管理****************************************************************/
    @LogAnnotation
    @PostMapping("getMsgContent")
    @ApiOperation(value = "分公司管理-列表")
    public R getList(CorporateBean o){
        List<CorporateBean> list = service.getList(o);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }

    @RequestMapping(value = "addInfo", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分公司管理-新增")
    public AjaxRes addInfo(@RequestBody CorporateBean bean) throws Exception {
        AjaxRes ar = new AjaxRes();
        int cb = service.addInfo(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
            if (cb == 5){
                ar.setFailMsg("手机号已存在");
            }
        }
        return  ar;
    }

    @RequestMapping(value = "getListById", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分公司管理-查询人员信息")
    public AjaxRes getListById(String id){
        AjaxRes ar = new AjaxRes();
        CorporateBean corporateBean = service.getListById(id);
        Map<String, Object> map = new HashMap<>(100);
        if(corporateBean !=null){
            map.put("corporateBean",corporateBean);
        }
        ar.setSucceed(map,GlobalConst.DATA_SUCCEED);
        return ar;
    }

    @LogAnnotation
    @PostMapping("switchListen")
    @ApiOperation(value = "分公司管理-权限锁定")
    public AjaxRes switchListen(CorporateBean bean) {
        AjaxRes ar = new AjaxRes();
        int result  = service.updateSwitchListen(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        }else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    @RequestMapping(value = "updateInfo", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分公司管理-修改")
    public AjaxRes updateInfo(@RequestBody CorporateBean bean) throws Exception {
        AjaxRes ar = new AjaxRes();
        int cb = service.updateInfo(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
//            if (cb == 5){
//                ar.setFailMsg("手机号已存在");
//            }
        }
        return  ar;
    }

    @LogAnnotation
    @PostMapping(value = "/getRole")
    @ApiOperation(value = "分公司管理-公司下拉框")
    public List<MapBean> getRole() {
        List<MapBean> result = service.getRole();
        return result;
    }


/******************************************************公司组织机构分公司管理****************************************************************/
    @LogAnnotation
    @PostMapping("getProjectContent")
    @ApiOperation(value = "项目部管理-列表")
    public R getProjectList(CorporateBean o){
        List<CorporateBean> list = service.getProjectList(o);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }

    @LogAnnotation
    @PostMapping("switchProject")
    @ApiOperation(value = "项目部管理-权限锁定")
    public AjaxRes switchProject(CorporateBean bean) {
        AjaxRes ar = new AjaxRes();
        int result  = service.updateswitchProject(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        }else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    @RequestMapping(value = "addProjectInfo", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分公司管理-新增")
    public AjaxRes addProjectInfo(@RequestBody CorporateBean bean) throws Exception {
        AjaxRes ar = new AjaxRes();
        int cb = service.addProjectInfo(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
            if (cb == 5){
                ar.setFailMsg("手机号已存在");
            }
        }
        return  ar;
    }

    @RequestMapping(value = "getListProject", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分公司管理-查询人员信息")
    public AjaxRes getListProject(String id){
        AjaxRes ar = new AjaxRes();
        CorporateBean corporateBean = service.getListProject(id);
        Map<String, Object> map = new HashMap<>(100);
        if(corporateBean !=null){
            map.put("corporateBean",corporateBean);
        }
        ar.setSucceed(map,GlobalConst.DATA_SUCCEED);
        return ar;
    }

    @RequestMapping(value = "updateProject", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分公司管理-修改")
    public AjaxRes updateProject(@RequestBody CorporateBean bean) throws Exception {
        AjaxRes ar = new AjaxRes();
        int cb = service.updateProject(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
        }
        return  ar;
    }


}

