package com.bonus.material.common.controller;

import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.material.back.domain.BackApplyInfo;
import com.bonus.material.basic.domain.BmProject;
import com.bonus.material.basic.domain.BmUnit;
import com.bonus.material.basic.mapper.BmUnitMapper;
import com.bonus.material.common.domain.dto.SelectDto;
import com.bonus.material.common.service.SelectService;
import com.bonus.material.materialStation.domain.ProAuthorizeInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 10488
 * 统一下拉选
 */
@Api(tags = "统一下拉选接口")
@RestController
@RequestMapping("/select/")
@Slf4j
public class SelectController {

    @Resource(name = "SelectService")
    private SelectService service;

    @Resource
    private BmUnitMapper bmUnitMapper;

    @ApiOperation(value = "往来单位下拉选")
    @PostMapping("getUnitList")
    public AjaxResult getUnitList(@RequestBody BmUnit bmUnit) {
        return service.getUnitList(bmUnit);
    }

    @ApiOperation(value = "往来单位下拉选，过滤掉班组类型")
    @PostMapping("getUnitListFilterTeam")
    public AjaxResult getUnitListFilterTeam(@RequestBody BmUnit bmUnit) {
        bmUnit.setEnableFilterTeam(true);
        return service.getUnitList(bmUnit);
    }

    /**
     * todo 领用申请往来单位下拉选专用
     * @param bmUnit
     * @return
     */
    @ApiOperation(value = "领用申请往来单位下拉选")
    @PostMapping("getUnitListLease")
    public AjaxResult getUnitListLease(@RequestBody BmUnit bmUnit) {
        try {
            return service.getUnitListLease(bmUnit);
        } catch (Exception e) {
            return AjaxResult.error("获取数据失败");
        }
    }

    /**
     * todo 领用申请往来单位授权人员下拉
     * @param bmUnit
     * @return
     */
    @ApiOperation(value = "领用申请往来单位授权人员下拉")
    @PostMapping("getUnitListLeasePerson")
    public AjaxResult getUnitListLeasePerson(@RequestBody BmUnit bmUnit) {
        try {
            return service.getUnitListLeasePerson(bmUnit);
        } catch (Exception e) {
            return AjaxResult.error("获取数据失败");
        }
    }

    @ApiOperation(value = "工程下拉选")
    @PostMapping("getProjectList")
    public AjaxResult getProjectList(@RequestBody BmProject bmProject) {
        try {
            return service.getProjectList(bmProject);
        } catch (Exception e) {
            log.error("获取数据失败");
            return AjaxResult.error("获取数据失败");
        }
    }

    /**
     * 领用工程下拉选
     * @param bmProject
     * @return
     */
    @ApiOperation(value = "领用工程下拉选")
    @PostMapping("getLeaseProjectList")
    public AjaxResult getLeaseProjectList(@RequestBody BmProject bmProject) {
        try {
            return service.getLeaseProjectList(bmProject);
        } catch (Exception e) {
            log.error("获取数据失败");
            return AjaxResult.error("获取数据失败");
        }
    }

    @ApiOperation(value = "机具类型下拉选")
    @PostMapping("getMaTypeData")
    public AjaxResult getMaTypeData(@RequestBody SelectDto dto){
        return service.getMaTypeData(dto);
    }

    @ApiOperation(value = "分部工程下拉选")
    @PostMapping("getBranchProject")
    public AjaxResult getBranchProject(){
        return service.getBranchProject();
    }

    @ApiOperation(value = "标准配置下拉选")
    @PostMapping("getConfigList")
    public AjaxResult getConfigList(@RequestBody BmUnit bmUnit) {
        return service.getConfigList(bmUnit);
    }

    /**
     * 机具类型下拉选
     * @param dto
     * @return
     */
    @ApiOperation(value = "机具类型下拉选")
    @PostMapping("getMaType")
    public AjaxResult getMaType(@RequestBody SelectDto dto){
        return service.getMaType(dto);
    }

//    @ApiOperation(value = "数据字典下拉选")
//    @PostMapping("getDictByPidCbx")
//    public AjaxResult getDictByPidCbx(@RequestBody SelectDto dto){
//        return service.getDictByPidCbx(dto);
//    }
//
//    @ApiOperation(value = "单位树/归属部门/所属上级")
//    @PostMapping("getDeptTree")
//    public AjaxResult getDeptTree(@RequestBody SelectDto dto){
//        return service.getDeptTree(dto);
//    }
//
//    @ApiOperation(value = "岗位下拉选")
//    @PostMapping("getPostCbx")
//    public AjaxResult getPostCbx(@RequestBody SelectDto dto){
//        return service.getPostCbx(dto);
//    }
//
//    @ApiOperation(value = "角色下拉选")
//    @PostMapping("getRoleCbx")
//    public AjaxResult getRoleCbx(@RequestBody SelectDto dto){
//        return service.getRoleCbx(dto);
//    }
//
//    @ApiOperation(value = "单位类型下拉选")
//    @PostMapping("getUnitTypeCbx")
//    public AjaxResult getUnitTypeCbx(@RequestBody SelectDto dto){
//        return service.getUnitTypeCbx(dto);
//    }

    @ApiOperation(value = "设备类型树")
    @PostMapping("getDeviceTypeTree")
    public AjaxResult getDeviceTypeTree(@RequestBody SelectDto dto){
        return service.getDeviceTypeTree(dto);
    }

    @ApiOperation(value = "设备类型树")
    @PostMapping("getDeviceTypeTreeTwo")
    public AjaxResult getDeviceTypeTreeTwo(@RequestBody SelectDto dto){
        return service.getDeviceTypeTreeTwo(dto);
    }

    @ApiOperation(value = "设备类型树--编码设备")
    @PostMapping("getDeviceTypeTreeThree")
    public AjaxResult getDeviceTypeTreeThree(@RequestBody SelectDto dto){
        return service.getDeviceTypeTreeThree(dto);
    }

    @ApiOperation(value = "退料设备类型树")
    @PostMapping("getBackDeviceTypeTree")
    public AjaxResult getBackDeviceTypeTree(@RequestBody SelectDto dto){
        return service.getBackDeviceTypeTree(dto);
    }

    @ApiOperation(value = "资产属性")
    @PostMapping("getAssetAttributesCbx")
    public AjaxResult getAssetAttributesCbx(@RequestBody SelectDto dto){
        return service.getAssetAttributesCbx(dto);
    }

    @ApiOperation(value = "机具厂家")
    @PostMapping("getDeviceFactoryCbx")
    public AjaxResult getDeviceFactoryCbx(@RequestBody SelectDto dto){
        return service.getDeviceFactoryCbx(dto);
    }

//    @ApiOperation(value = "工程项目")
//    @PostMapping("getProCbx")
//    public AjaxResult getProCbx(@RequestBody SelectDto dto){
//        return service.getProCbx(dto);
//    }

    @ApiOperation(value = "配件所属上级树")
    @PostMapping("getAccessoryTree")
    public AjaxResult getAccessoryTree(){
        return service.getAccessoryTree();
    }

    @ApiOperation(value = "配件所属上级树")
    @PostMapping("getPartTree")
    public AjaxResult getPartTree(@RequestBody SelectDto dto){
        return service.getPartTree(dto);
    }

    @ApiOperation(value = "货架")
    @PostMapping("getGoodsShelvesCbx")
    public AjaxResult getGoodsShelvesCbx(@RequestBody SelectDto dto){
        return service.getGoodsShelvesCbx(dto);
    }

//    @ApiOperation(value = "用户/维修员/库管员/采购员")
//    @PostMapping("getUserByRoleIdCbx")
//    public AjaxResult getUserByRoleIdCbx(@RequestBody SelectDto dto){
//        return service.getUserByRoleIdCbx(dto);
//    }

    @ApiOperation(value = "往来单位id和标段工程id获取协议信息")
    @PostMapping("getAgreementInfoById")
    public AjaxResult getAgreementInfoById(@RequestBody SelectDto dto){
        return service.getAgreementInfoById(dto);
    }

    @ApiOperation(value = "材料站退料往来单位id和标段工程id获取协议信息")
    @PostMapping("getAgreementInfoByIdBack")
    public AjaxResult getAgreementInfoByIdBack(@RequestBody SelectDto dto){
        return service.getAgreementInfoByIdBack(dto);
    }

    @ApiOperation(value = "在用设备类型树")
    @PostMapping("/getUseTypeTree")
    public AjaxResult getUseTypeTree(@RequestBody BackApplyInfo bean) {
        BmUnit queryUnitInfo = bmUnitMapper.selectBmUnitByUnitId(bean.getUnitId());
        if (queryUnitInfo == null) {
            throw new ServiceException("单位不存在");
        }
        if (queryUnitInfo.getTypeId() == null) {
            throw new ServiceException("单位类型为空");
        }
        if (queryUnitInfo.getTypeId() == 1731L) {
            // 获取班组在用量
            return service.getBzUseTypeTree(bean);
        }
        return service.getUseTypeTree(bean);
    }

    @ApiOperation(value = "在用设备类型")
    @PostMapping("/getUseType")
    public AjaxResult getUseType(@RequestBody BackApplyInfo bean) {
        return service.getUseType(bean);
    }

    @ApiOperation(value = "班组下拉选")
    @PostMapping("getTeamList")
    public AjaxResult getTeamList(@RequestBody ProAuthorizeInfo bean) {
        return service.getTeamList(bean);
    }

    @ApiOperation(value = "工程下拉选")
    @PostMapping("getProjectInfo")
    public AjaxResult getProjectInfo(@RequestBody BmProject bmProject) {
        try {
            return service.getProjectInfo(bmProject);
        } catch (Exception e) {
            log.error("获取工程信息异常", e);
            return AjaxResult.error("获取工程信息异常");
        }
    }
}
