package com.qmth.sop.server.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.sop.business.entity.SysSupplier;
import com.qmth.sop.business.service.SysSupplierService;
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.SupplierTypeEnum;
import com.qmth.sop.common.enums.UserRoleEnum;
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.Objects;

/**
 * 供应商表 控制器.
 *
 * @author: shudonghui
 * @date: 2023-08-02 14:18:39
 * @version: 1.0
 * @email: shudonghui@qmth.com.cn
 * @Company: www.qmth.com.cn
 */
@Api(tags = "供应商表 Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_SUPPLIER)
public class SysSupplierController {

    @Resource
    SysSupplierService sysSupplierService;

    @ApiOperation(value = "供应商表查询接口")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "供应商表列表信息", response = SysSupplier.class)})
    public Result query(
            @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) {
        IPage<SysSupplier> resultIPage = sysSupplierService.query(new Page<>(pageNumber, pageSize));

        return ResultUtil.ok(resultIPage);
    }

    @ApiOperation(value = "新增供应商表接口")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "新增供应商表信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.ADD)
    public Result add(@Valid @ApiParam(value = "供应商表信息", required = true) @RequestBody(required = true) SysSupplier sysSupplier, BindingResult bindingResult) throws InterruptedException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        sysSupplierService.add(sysSupplier);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "修改供应商表接口")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "修改供应商表信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result update(@Valid @ApiParam(value = "供应商表信息", required = true) @RequestBody(required = true) SysSupplier sysSupplier, BindingResult bindingResult) throws InterruptedException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        sysSupplierService.update(sysSupplier);
        return ResultUtil.ok();
    }

    //    @ApiOperation(value = "删除供应商表接口")
//    @RequestMapping(value = "/delete", method = RequestMethod.POST)
//    @ApiResponses({@ApiResponse(code = 200, message = "删除供应商表信息", response = EditResult.class)})
//    public Result delete(@ApiParam(value = "供应商表信息", required = true) @RequestParam(required = true) SysSupplier sysSupplier) {
//        sysSupplierService.delete(sysSupplier.getId());
//        return ResultUtil.ok(new EditResult(sysSupplier.getId()));
//    }
    /*
     *启用禁用
     */
    @ApiOperation(value = "启用禁用供应商表接口")
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "启用禁用供应商表信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result enable(@ApiParam(value = "供应商表信息", required = true) @RequestParam(required = true) long id) {
        sysSupplierService.enable(id);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "查询供应商表信息接口")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "供应商表信息", response = SysSupplier.class)})
    public Result list(@ApiParam(value = "是否启用", required = false) @RequestParam(required = false) Boolean enable,
                       @ApiParam(value = "类型", required = false) @RequestParam(required = false) SupplierTypeEnum type) {
        return ResultUtil.ok(sysSupplierService.list(new QueryWrapper<SysSupplier>().lambda().eq(Objects.nonNull(enable), SysSupplier::getEnable, enable).eq(Objects.nonNull(type), SysSupplier::getType, type)));
    }

    @ApiOperation(value = "获取单个供应商表信息接口")
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "供应商表信息", response = SysSupplier.class)})
    public Result info(@ApiParam(value = "供应商表id", required = true) @RequestParam long id) {
        return ResultUtil.ok(sysSupplierService.getById(id));
    }

    @ApiOperation(value = "根据用户角色查询供应商信息接口")
    @RequestMapping(value = "/list/by/user_role", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "供应商表信息", response = SysSupplier.class)})
    public Result listByUserRole(@ApiParam(value = "用户角色", required = true) @RequestParam UserRoleEnum userRoleEnum) {
        return ResultUtil.ok(sysSupplierService.findByUserRoleEnum(userRoleEnum));
    }
}