package com.qmth.sop.server.api;

import com.alibaba.fastjson.JSON;
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.bean.result.SysLevelResult;
import com.qmth.sop.business.entity.SysLevel;
import com.qmth.sop.business.service.SysLevelRoleService;
import com.qmth.sop.business.service.SysLevelService;
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 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:37:24
 * @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_LEVEL)
public class SysLevelController {


    @Resource
    SysLevelService sysLevelService;


    @Resource
    SysLevelRoleService sysLevelRoleService;


    @ApiOperation(value = "档位表查询接口")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "档位表列表信息", response = SysLevelResult.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<SysLevelResult> resultIPage = sysLevelService.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) SysLevelResult sysLevelResult, BindingResult bindingResult) throws InterruptedException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        sysLevelService.add(sysLevelResult);
        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) SysLevelResult sysLevelResult, BindingResult bindingResult) throws InterruptedException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        sysLevelService.update(sysLevelResult);
        return ResultUtil.ok();
    }


    @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 = "档位表信息", required = true) @RequestParam(required = true) long id) {
        sysLevelService.delete(id);
        return ResultUtil.ok();
    }

    /**
     * 获取档位表列表
     *
     * @return
     */

    @ApiOperation(value = "获取档位表列表接口")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "档位表列表", response = SysLevel.class)})
    public Result list(@ApiParam(value = "是否启用", required = false) @RequestParam(required = false) Boolean enable,
                       @ApiParam(value = "类型", required = false) @RequestParam(required = false) ProductTypeEnum type) {
        return ResultUtil.ok(sysLevelService.list(new QueryWrapper<SysLevel>().lambda().eq(Objects.nonNull(enable), SysLevel::getEnable, enable).eq(Objects.nonNull(type), SysLevel::getType, type)));
    }

    /**
     * 获取单个
     *
     * @param id
     * @return
     */

    @ApiOperation(value = "获取单个档位表接口")
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "档位表", response = SysLevelResult.class)})
    public Result get(@ApiParam(value = "档位表id", required = true) @RequestParam long id) {
        SysLevel sysLevel = sysLevelService.getById(id);
        SysLevelResult sysLevelResult = JSON.toJavaObject((JSON) JSON.toJSON(sysLevel), SysLevelResult.class);
        sysLevelResult.setRoleList(sysLevelRoleService.getList(sysLevelResult.getId()));
        return ResultUtil.ok(sysLevelResult);
    }


}