package com.qmth.sop.server.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.sop.business.bean.params.CustomReceiverParam;
import com.qmth.sop.business.bean.result.SysCustomReceiverResult;
import com.qmth.sop.business.entity.SysCustomReceiver;
import com.qmth.sop.business.entity.SysUser;
import com.qmth.sop.business.service.SysCustomReceiverService;
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.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;

/**
 * @Description 客户收件信息控制类
 * @Author haoguanghui
 * @date 2024/09/04
 */
@Api(tags = "客户收件信息表Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_CUSTOM_RECEIVER)
public class SysCustomReceiverController {

    @Resource
    SysCustomReceiverService sysCustomReceiverService;


    @ApiOperation(value = "客户列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "返回信息", response = SysCustomReceiver.class) })
    public Result list(@ApiParam(value = "客户类型", required = false) @RequestParam(required = false) ProductTypeEnum type) {
        return ResultUtil.ok(sysCustomReceiverService.listCustom(type));
    }

    @ApiOperation(value = "收件信息分页")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "收件信息分页", response = SysCustomReceiverResult.class) })
    public Result page(
            @ApiParam(value = "客户类型", required = false) @RequestParam(required = false) ProductTypeEnum type,
            @ApiParam(value = "客户名称", required = false) @RequestParam(required = false) String name,
            @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(sysCustomReceiverService.pageReceiver(new Page<>(pageNumber, pageSize), type, name));
    }

    @ApiOperation(value = "收件信息保存/修改")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "保存成功", response = Result.class) })
    @OperationLog(logType = LogTypeEnum.EDIT)
    public Result saveCustomReceiver(@Valid @RequestBody CustomReceiverParam param, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(sysCustomReceiverService.saveCustomReceiver(param, requestUser));
    }

    @ApiOperation(value = "收件信息删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "删除收件信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.DELETE)
    public Result delete(@ApiParam(value = "id", required = true) @RequestParam(required = true) long id) {
        sysCustomReceiverService.removeById(id);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "收件信息导出")
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "返回信息", response = Object.class) })
    @OperationLog(logType = LogTypeEnum.EXPORT)
    public void export(@ApiParam(value = "客户类型", required = false) @RequestParam(required = false) ProductTypeEnum type,
            @ApiParam(value = "客户名称", required = false) @RequestParam(required = false) String name) throws Exception {
        sysCustomReceiverService.export(type,name);
    }

}
