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

import com.bonus.hn.bearing.capacity.backstage.companypro.entity.ProjectManagerBean;
import com.bonus.hn.bearing.capacity.backstage.manager.annotation.LogAnnotation;
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 com.bonus.hn.bearing.capacity.backstage.select.entity.DataDictBean;
import com.bonus.hn.bearing.capacity.backstage.teamset.entity.ResourceSetBean;
import com.bonus.hn.bearing.capacity.backstage.teamset.entity.StringDataBean;
import com.bonus.hn.bearing.capacity.backstage.teamset.service.TeamSetService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xliu
 * @date 2023/11/6 15:37  项目管理团队设置
 */
@RestController
@RequestMapping("/teamSet")
public class TeamSetController {

    @Resource
    private TeamSetService teamSetService;

    @LogAnnotation
    @PostMapping("updateTeamSetStatus")
    @ApiOperation(value = "修改分公司启用状态")
    public R updateTeamSetStatus(ResourceSetBean bean){
        int num = teamSetService.updateTeamSetStatus(bean);
        return num > 0 ? R.okTable("修改成功", num):R.failTable("修改失败");
    }

    @LogAnnotation
    @PostMapping("getTeamSetByType")
    @ApiOperation(value = "根据类型查找分公司设置")
    public R getTeamSetByType(ResourceSetBean bean){
        List<ResourceSetBean> list = teamSetService.getTeamSetByType(bean);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }


    @LogAnnotation
    @PostMapping("getTeamByLevelAndName")
    @ApiOperation(value = "根据电业等级和名称查询分公司团队")
    public R getTeamByLevelAndName(ResourceSetBean bean){
        System.err.println("bean="+bean);
        int num = teamSetService.getTeamByLevelAndName(bean);
        System.err.println("bean="+num);
        return num <= 0 ? R.okTable("允许添加", num):R.failTable("失败");
    }


    @LogAnnotation
    @PostMapping("getTeamByTeamTypeAndName")
    @ApiOperation(value = "根据电业等级和名称查询分公司团队")
    public R getTeamByTeamTypeAndName(ResourceSetBean bean){
        System.err.println("bean="+bean);
        int num = teamSetService.getTeamByTeamTypeAndName(bean);
        System.err.println("bean="+num);
        return num <= 0 ? R.okTable("允许添加", num):R.failTable("失败");
    }

    @LogAnnotation
    @PostMapping("addBranchOfficeTeamSet")
    @PreAuthorize("hasAuthority('sysTeamSetByType:add')")
    @ApiOperation(value = "新增分公司团队配置")
    @Transactional
    public R addBranchOfficeTeamSet(StringDataBean bean){
        System.err.println("bean="+bean.getStringKey());
        int num = teamSetService.addBranchOfficeTeamSet(bean);
        System.err.println("bean="+num);
        return num > 0 ? R.okTable("添加成功", num):R.failTable("添加失败");
    }


    @LogAnnotation
    @PostMapping("getDataDictByDickValModify")
    @ApiOperation(value = "分公司团队设置修改查询")
    public List<DataDictBean> getDataDictByDickValModify(DataDictBean o) {
        System.err.println("DataDictBean="+o);
        List<DataDictBean> list = teamSetService.getDataDictByDickValModify(o);
        System.err.println("list="+list);
        if (list.size() <= 0) {
            list = new ArrayList<>();
        }
        return list;
    }


    @LogAnnotation
    @PostMapping("delTeamSet")
    @ApiOperation(value = "分公司管理团队-删除功能")
    public AjaxRes delTeamSet(StringDataBean bean) {
        System.err.println("delTeamSet=="+bean);
        AjaxRes ar = new AjaxRes();
        int result  = teamSetService.delTeamSet(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        } else if(result==3){
            //有班组已在使用此类型
            ar.setSucceedMsg("已在使用");
        } else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    @LogAnnotation
    @PostMapping("delDataDict")
    @ApiOperation(value = "数据字典-根据id删除功能")
    public AjaxRes delDataDict(StringDataBean bean) {
        System.err.println("delDataDict=="+bean);
        AjaxRes ar = new AjaxRes();
        //查看是否已有配置的且未解绑的班组 功能待实现

        int result  = teamSetService.delDataDict(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.DEL_SUCCEED);
        }else {
            ar.setFailMsg(GlobalConst.DEL_FAIL);
        }
        return ar;
    }

    @LogAnnotation
    @PostMapping("saveDataDict")
    @ApiOperation(value = "保存数据字段字段和字段顺序")
    public AjaxRes saveDataDict(StringDataBean bean) {
        System.err.println("delDataDict=="+bean);
        AjaxRes ar = new AjaxRes();
        int result  = teamSetService.saveDataDict(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.SAVE_SUCCEED);
        }else {
            ar.setFailMsg(GlobalConst.SAVE_FAIL);
        }
        return ar;
    }

}
