package com.qmth.sop.server.api;


import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.sop.common.annotation.OperationLog;
import com.qmth.sop.business.bean.params.ServiceScopeParam;
import com.qmth.sop.business.bean.result.CrmServiceResult;
import com.qmth.sop.business.service.TBCrmService;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.LogTypeEnum;
import com.qmth.sop.common.enums.ProductTypeEnum;
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 = "服务范围管理前端控制器")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_SERVICE_SERVICE_SCOPE)
public class TBServiceScopeController {
    @Resource
    private TBCrmService tbCrmService;

    @ApiOperation(value = "服务范围管理-分页查询")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = CrmServiceResult.class)})
    public Result findServiceScopePage(@ApiParam(value = "服务单元id") @RequestParam(required = false) String serviceUnitId,
                                       @ApiParam(value = "城市") @RequestParam(required = false) String city,
                                       @ApiParam(value = "客户类型") @RequestParam(required = false) ProductTypeEnum productType,
                                       @ApiParam(value = "客户名称") @RequestParam(required = false) String customName,
                                       @ApiParam(value = "派单绑定状态") @RequestParam(required = false) Boolean bindStatus,
                                       @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(tbCrmService.findServiceScopePage(SystemConstant.convertIdToLong(serviceUnitId), city, productType, customName, bindStatus, pageNumber, pageSize));
    }

    @ApiOperation(value = "服务范围管理-新增服务范围-未绑定派单分页查询")
    @RequestMapping(value = "/unbind/page", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = CrmServiceResult.class)})
    public Result findUnbindOrderPage(@ApiParam(value = "派单人") @RequestParam(required = false) String crmUserId,
                                      @ApiParam(value = "客户类型") @RequestParam(required = false) ProductTypeEnum productType,
                                      @ApiParam(value = "客户名称") @RequestParam(required = false) String customName,
                                      @ApiParam(value = "项目单号") @RequestParam(required = false) String crmNo,
                                      @ApiParam(value = "派单时间-起始") @RequestParam(required = false) Long startTime,
                                      @ApiParam(value = "派单时间-截止") @RequestParam(required = false) Long endTime,
                                      @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(tbCrmService.findUnbindOrderPage(SystemConstant.convertIdToLong(crmUserId), productType, customName, crmNo, startTime, endTime, pageNumber, pageSize));
    }

    @ApiOperation(value = "服务范围管理-批量划定派单和服务单元关系")
    @RequestMapping(value = "/bind_batch", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = Result.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result bindCrmWithServiceBatch(@Valid @RequestBody ServiceScopeParam serviceScopeParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        tbCrmService.bindCrmWithServiceBatch(serviceScopeParam);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "服务范围管理-移出")
    @RequestMapping(value = "/unbind", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = Result.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result unbind(@ApiParam(value = "派单id") @RequestParam(required = false) String crmId) {
        tbCrmService.unbindCrmWithService(SystemConstant.convertIdToLong(crmId));
        return ResultUtil.ok();
    }

    @ApiOperation(value = "服务范围管理-派单小计")
    @RequestMapping(value = "/subTotal", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findCrmSubTotalData(@ApiParam(value = "服务单元id") @RequestParam(required = false) String serviceUnitId,
                                      @ApiParam(value = "城市") @RequestParam(required = false) String city,
                                      @ApiParam(value = "客户类型") @RequestParam(required = false) ProductTypeEnum productType,
                                      @ApiParam(value = "客户名称") @RequestParam(required = false) String customName,
                                      @ApiParam(value = "派单绑定状态") @RequestParam(required = false) Boolean bindStatus) {
        return ResultUtil.ok(tbCrmService.findCrmSubTotalData(SystemConstant.convertIdToLong(serviceUnitId), city, productType, customName, bindStatus));
    }
}