package com.qmth.sop.server.api;


import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.sop.business.bean.params.AutoAllocationParam;
import com.qmth.sop.business.bean.params.UserArchivesAllocationParam;
import com.qmth.sop.business.bean.result.ArchivesSourceResult;
import com.qmth.sop.business.bean.result.CrmArchivesAllocationResult;
import com.qmth.sop.business.bean.result.UserArchivesAllocationResult;
import com.qmth.sop.business.bean.result.UserArchivesAllocationSubTotalResult;
import com.qmth.sop.business.service.TBUserArchivesAllocationService;
import com.qmth.sop.business.service.TBUserArchivesService;
import com.qmth.sop.common.annotation.OperationLog;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.LogTypeEnum;
import com.qmth.sop.common.enums.RoleTypeEnum;
import com.qmth.sop.common.util.Result;
import com.qmth.sop.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

/**
 * <p>
 * 人员分配表 前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2023-08-01
 */

@Api(tags = "人员调配Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_USER_ARCHIVES_ALLOCATION)
public class TBUserArchivesAllocationController {
    @Resource
    private TBUserArchivesAllocationService tbUserArchivesAllocationService;
    @Resource
    private TBUserArchivesService tbUserArchivesService;

    @ApiOperation(value = "人员调配管理-分页查询")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = UserArchivesAllocationResult.class)})
    public Result findUserArchivesAllocationPage(@ApiParam(value = "服务单元id") @RequestParam(required = false) String serviceUnitId,
                                                 @ApiParam(value = "省") @RequestParam(required = false) String province,
                                                 @ApiParam(value = "市") @RequestParam(required = false) String city,
                                                 @ApiParam(value = "县") @RequestParam(required = false) String area,
                                                 @ApiParam(value = "客户名称") @RequestParam(required = false) String customName,
                                                 @ApiParam(value = "分配差额") @RequestParam(required = false) Integer gap,
                                                 @ApiParam(value = "大区id") @RequestParam(required = false) Long regionId,
                                                 @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                                                 @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {

        return ResultUtil.ok(tbUserArchivesAllocationService.findCrmAllocationPage(SystemConstant.convertIdToLong(serviceUnitId), province, city, area, customName, gap, regionId, pageNumber, pageSize));
    }

    @ApiOperation(value = "人员调配管理-小计查询")
    @RequestMapping(value = "/sub_total", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = UserArchivesAllocationSubTotalResult.class)})
    public Result findUserArchivesAllocationSubTotal(@ApiParam(value = "服务单元id") @RequestParam(required = false) String serviceUnitId,
                                                     @ApiParam(value = "省") @RequestParam(required = false) String province,
                                                     @ApiParam(value = "市") @RequestParam(required = false) String city,
                                                     @ApiParam(value = "县") @RequestParam(required = false) String area,
                                                     @ApiParam(value = "客户名称") @RequestParam(required = false) String customName,
                                                     @ApiParam(value = "分配差额") @RequestParam(required = false) Integer gap,
                                                     @ApiParam(value = "大区id") @RequestParam(required = false) Long regionId) {

        return ResultUtil.ok(tbUserArchivesAllocationService.findCrmAllocationSubTotal(SystemConstant.convertIdToLong(serviceUnitId), province, city, area, customName, gap, regionId));
    }

    @ApiOperation(value = "人员调配管理-根据派单号查询派单分配信息")
    @RequestMapping(value = "/detail_by_crm_no", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = CrmArchivesAllocationResult.class)})
    public Result findDetailByCrmNo(@ApiParam(value = "派单号", required = true) @RequestParam String crmNo) {
        return ResultUtil.ok(tbUserArchivesAllocationService.findAllocationByCrmNo(crmNo));
    }

    @ApiOperation(value = "人员调配管理-调配")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = Result.class)})
    @OperationLog(logType = LogTypeEnum.EDIT)
    public Result edit(@Valid @RequestBody UserArchivesAllocationParam userArchivesAllocationParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        tbUserArchivesAllocationService.editCrmAllocation(userArchivesAllocationParam);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "人员调配管理-自动调配")
    @RequestMapping(value = "/auto_edit", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = Result.class)})
    @OperationLog(logType = LogTypeEnum.EDIT)
    public Result autoEdit(@Valid @RequestBody AutoAllocationParam allocationParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        tbUserArchivesAllocationService.autoEditCrmAllocationBatch(allocationParam);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "人员调配管理-发布")
    @RequestMapping(value = "/publish", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "发布成功", response = Result.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result publishCrmSop(@ApiParam(value = "派单号", required = true) @RequestParam String crmNo) throws InterruptedException {
        tbUserArchivesAllocationService.publishSop(crmNo);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "人员调配管理-撤销发布")
    @RequestMapping(value = "/un_publish", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "作废成功", response = Result.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result unPublishCrmSop(@ApiParam(value = "派单号", required = true) @RequestParam String crmNo) {
        tbUserArchivesAllocationService.unPublishSop(crmNo);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "人员调配管理-查询空闲工程师")
    @RequestMapping(value = "/free_engineer", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = ArchivesSourceResult.class)})
    public Result findFreeEngineer(@ApiParam(value = "工程师类型", required = true) @RequestParam RoleTypeEnum roleType,
                                   @ApiParam(value = "派单号") @RequestParam(required = false) String crmNo) {
        return ResultUtil.ok(tbUserArchivesService.findFreeEngineerSourceByType(roleType, crmNo));
    }

    @ApiOperation(value = "人员调配管理-查询空闲区域协调人")
    @RequestMapping(value = "/free_coordinator", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = ArchivesSourceResult.class)})
    public Result findFreeCoordinator(@ApiParam(value = "服务单元id", required = true) @RequestParam String serviceUnitId,
                                      @ApiParam(value = "派单号") @RequestParam(required = false) String crmNo) {
        return ResultUtil.ok(tbUserArchivesService.findFreeCoordinatorByServiceId(SystemConstant.convertIdToLong(serviceUnitId), crmNo, false));
    }
}
