package com.qmth.sop.server.api;


import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.sop.business.bean.params.ServiceUnitParam;
import com.qmth.sop.business.bean.result.ServiceUnitResult;
import com.qmth.sop.business.entity.SysUser;
import com.qmth.sop.business.service.TBServiceService;
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.ProductTypeEnum;
import com.qmth.sop.common.enums.ServiceStatusEnum;
import com.qmth.sop.common.util.Result;
import com.qmth.sop.common.util.ResultUtil;
import com.qmth.sop.common.util.ServletUtil;
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_UNIT)
public class TBServiceController {
    @Resource
    private TBServiceService tbServiceService;

    @ApiOperation(value = "服务单元管理-分页查询")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = ServiceUnitResult.class)})
    public Result findServiceUnitPage(@ApiParam(value = "业务类型") @RequestParam(required = false) ProductTypeEnum type,
                                      @ApiParam(value = "负责人") @RequestParam(required = false) String serviceLeadId,
                                      @ApiParam(value = "服务状态") @RequestParam(required = false) ServiceStatusEnum status,
                                      @ApiParam(value = "起始时间") @RequestParam(required = false) Long createStartTime,
                                      @ApiParam(value = "截止时间") @RequestParam(required = false) Long createEndTime,
                                      @ApiParam(value = "服务单元名称") @RequestParam(required = false) String serviceUnitName,
                                      @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(tbServiceService.page(type, SystemConstant.convertIdToLong(serviceLeadId), status, createStartTime, createEndTime, serviceUnitName, pageNumber, pageSize));
    }

    @ApiOperation(value = "服务单元管理-编辑")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = Result.class)})
    @OperationLog(logType = LogTypeEnum.EDIT)
    public Result editServiceUnit(@Valid @RequestBody ServiceUnitParam serviceUnitParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(tbServiceService.edit(serviceUnitParam, requestUser));
    }

    @ApiOperation(value = "服务单元管理-作废")
    @RequestMapping(value = "/cancel", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "作废成功", response = Result.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result cancelServiceUnit(@ApiParam(value = "服务单元id", required = true) @RequestParam String id) {
        tbServiceService.cancelServiceUnit(SystemConstant.convertIdToLong(id));
        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 publishServiceUnit(@ApiParam(value = "服务单元id", required = true) @RequestParam String id) {
        tbServiceService.publishServiceUnit(SystemConstant.convertIdToLong(id));
        return ResultUtil.ok();
    }

    @ApiOperation(value = "服务单元管理-关闭")
    @RequestMapping(value = "/cloze", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "作废成功", response = Result.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result clozeServiceUnit(@ApiParam(value = "服务单元id", required = true) @RequestParam String id) {
        tbServiceService.clozeServiceUnit(SystemConstant.convertIdToLong(id));
        return ResultUtil.ok();
    }

    @ApiOperation(value = "服务单元管理-重启")
    @RequestMapping(value = "/restart", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "重启成功", response = Result.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    @Deprecated
    public Result restartServiceUnit(@ApiParam(value = "服务单元id", required = true) @RequestParam String id) {
        tbServiceService.restartServiceUnit(SystemConstant.convertIdToLong(id));
        return ResultUtil.ok();
    }
}
