package com.qmth.sop.server.api;


import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.sop.business.bean.params.ServiceRegionParam;
import com.qmth.sop.business.bean.result.ServiceRegionResult;
import com.qmth.sop.business.service.TBServiceRegionService;
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.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;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务区域规划表 前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2023-08-01
 */
@Api(tags = "服务区域规划前端控制器")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_SERVICE_SERVICE_REGION)
public class TBServiceRegionController {
    @Resource
    private TBServiceRegionService tbServiceRegionService;

    @ApiOperation(value = "服务区域规划-分页查询")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = ServiceRegionResult.class)})
    public Result findServiceRegionPage(@ApiParam(value = "服务单元id") @RequestParam(required = false) String serviceUnitId,
                                        @ApiParam(value = "大区名称") @RequestParam(required = false) String regionName,
                                        @ApiParam(value = "大区经理") @RequestParam(required = false) String leadId,
                                        @ApiParam(value = "城市") @RequestParam(required = false) String city,
                                        @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(tbServiceRegionService.findServiceRegionPage(SystemConstant.convertIdToLong(serviceUnitId), regionName, SystemConstant.convertIdToLong(leadId), city, pageNumber, pageSize));
    }

    @ApiOperation(value = "服务区域规划-编辑")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = Result.class)})
    @OperationLog(logType = LogTypeEnum.EDIT)
    public Result bindCrmWithServiceBatch(@Valid @RequestBody ServiceRegionParam serviceRegionParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(tbServiceRegionService.editServiceRegion(serviceRegionParam));
    }

    @ApiOperation(value = "服务区域规划-删除")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "删除成功", response = ServiceRegionResult.class)})
    @OperationLog(logType = LogTypeEnum.DELETE)
    public Result removeServiceRegion(@ApiParam(value = "服务单元id") @RequestParam(required = false) List<String> serviceRegionIdList) {
        List<Long> idList = serviceRegionIdList.stream().map(Long::parseLong).collect(Collectors.toList());
        tbServiceRegionService.removeServiceRegion(idList);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "服务区域规划-查询可选大区经理")
    @RequestMapping(value = "/find_leader", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = ServiceRegionResult.class)})
    public Result findCanChooseLeader(@ApiParam(value = "服务单元id") @RequestParam(required = false) String serviceUnitId) {
        return ResultUtil.ok(tbServiceRegionService.findCanChooseLeader(SystemConstant.convertIdToLong(serviceUnitId)));
    }
}
