package cn.qinys.platform.upms.api.controller;

import cn.qinys.platform.base.exceptions.BizException;
import cn.qinys.platform.base.response.Result;
import cn.qinys.platform.entity.upms.SystemParams;
import cn.qinys.platform.upms.api.mapper.SystemParamsMapper;
import cn.qinys.platform.upms.api.req.SystemParamsCreateReq;
import cn.qinys.platform.upms.api.req.SystemParamsPageReq;
import cn.qinys.platform.upms.api.req.SystemParamsRemoveReq;
import cn.qinys.platform.upms.api.req.SystemParamsUpdateReq;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 系统参数配置
 *
 * @author tanlie
 * Date: 2024/4/2 17:26
 */
@AllArgsConstructor
@RestController
@RequestMapping("/upms/system-params")
public class SystemParamsController {

    private final SystemParamsMapper paramsMapper;

    /**
     * 系统参数分页
     *
     * @param req
     * @return
     */
    @GetMapping("/page")
    public Result<Page<SystemParams>> getPage(SystemParamsPageReq req) {
        req = req == null ? new SystemParamsPageReq() : req;
        Page<SystemParams> page = new Page<>(req.getCurrent(), req.getSize());
        QueryWrapper<SystemParams> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasLength(req.getCode()), "code", req.getCode());
        wrapper.like(StringUtils.hasLength(req.getValue()), "value", req.getValue());
        wrapper.like(StringUtils.hasLength(req.getRemark()), "remark", req.getRemark());
        wrapper.orderByDesc("id");
        Page<SystemParams> pageVo = paramsMapper.selectPage(page, wrapper);
        return new Result<>(pageVo);
    }


    /**
     * 修改系统参数
     *
     * @param req
     * @return
     */
    @PostMapping("/update")
    public Result<String> updateParams(@RequestBody @Valid SystemParamsUpdateReq req) {
        // 防重
        QueryWrapper<SystemParams> wrapper = new QueryWrapper<>();
        wrapper.eq("code", req.getCode());
        wrapper.eq("value", req.getValue());
        wrapper.last("LIMIT 1");
        SystemParams exist = paramsMapper.selectOne(wrapper);
        if (exist != null && !exist.getId().equals(req.getId())) {
            throw new BizException("同code同value的数据已存在");
        }
        UpdateWrapper<SystemParams> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", req.getId());
        SystemParams entity = new SystemParams();
        entity.setCode(req.getCode());
        entity.setValue(req.getValue());
        entity.setRemark(req.getRemark());
        entity.setUpdatedAt(new Date());
        paramsMapper.update(entity, updateWrapper);
        return new Result<>("操作成功");
    }

    /**
     * 新增系统参数
     *
     * @param req
     * @return
     */
    @PostMapping("/create")
    public Result<String> createParams(@RequestBody @Valid SystemParamsCreateReq req) {
        // 防重
        QueryWrapper<SystemParams> wrapper = new QueryWrapper<>();
        wrapper.eq("code", req.getCode());
        wrapper.eq("value", req.getValue());
        wrapper.last("LIMIT 1");
        SystemParams exist = paramsMapper.selectOne(wrapper);
        if (exist != null) {
            throw new BizException("同code同value的数据已存在");
        }
        SystemParams entity = new SystemParams();
        entity.setCode(req.getCode());
        entity.setValue(req.getValue());
        entity.setRemark(req.getRemark());
        entity.setUpdatedAt(new Date());
        entity.setCreatedAt(new Date());
        paramsMapper.insert(entity);
        return new Result<>("操作成功");
    }

    /**
     * 删除参数
     *
     * @param req
     * @return
     */
    @PostMapping("/remove")
    public Result<String> removeParams(@RequestBody @Valid SystemParamsRemoveReq req) {
        paramsMapper.deleteBatchIds(req.getIds());
        return new Result<>("操作成功");
    }


}
