package com.persagy.dye.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.persagy.common.utils.DateUtil;
import com.persagy.common.utils.ResponseResult;
import com.persagy.common.utils.ResponseUtil;
import com.persagy.dye.pojo.dto.DyeRuleCustom;
import com.persagy.dye.service.IDyeRuleCustomService;
import com.persagy.dye.service.impl.DyeSdkServiceImpl;

import cn.hutool.core.collection.CollectionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 染色规则自定义表
 *
 * @author yangwanyi
 * @version v1.0.0
 * @company persagy
 * @date 2021-10-25 15:58:56
 */

@Api(tags = "染色规则自定义表")
@RestController
@RequestMapping(value = "/dyeRuleCustom", method = RequestMethod.POST)
public class DyeRuleCustomController {

    @Autowired
    private IDyeRuleCustomService dyeRuleCustomService;

    @Autowired
    private DyeSdkServiceImpl dyeSdkService;

    /**
     * 列表查询
     */
    @ApiOperation(value = "列表查询")
    @RequestMapping(value = "queryDyeRuleCustomList")
    public ResponseResult queryDyeRuleCustomList(@RequestBody DyeRuleCustom dyeRuleCustom) {
        LambdaQueryWrapper<DyeRuleCustom> queryWrapper = new DyeRuleCustom.BuilderQueryWrapper().builder();
        queryWrapper.orderByAsc(DyeRuleCustom::getBasicId);
        queryWrapper.orderByAsc(DyeRuleCustom::getOrderNum);
		List<DyeRuleCustom> list = this.dyeRuleCustomService.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return ResponseUtil.successResult(Lists.newArrayList(), 0L);
        }
        for (DyeRuleCustom customRule : list) {
            customRule.setTempUpperLimit(this.dyeSdkService.replace99ToBoundless(customRule.getUpperLimit() + customRule.getUpperSymbol()));
            customRule.setTempLowerLimit(this.dyeSdkService.replace99ToBoundless(customRule.getLowerSymbol() + customRule.getLowerLimit()));
        }
        return ResponseUtil.successResult(list, (long) list.size());
    }

    /**
     * 新增
     */
    @ApiOperation(value = "保存")
    @RequestMapping(value = "createDyeRuleCustom")
    public ResponseResult createDyeRuleCustom(@RequestBody DyeRuleCustom dyeRuleCustom) {
        dyeRuleCustom.setCreateTime(DateUtil.format(new Date(), DateUtil.FORMAT_DATE_YYYY_MM_DD_HH_MM_SS));
        dyeRuleCustom.setUpperLimit(this.dyeSdkService.replaceBoundlessTo99(dyeRuleCustom.getTempUpperLimit()));
        dyeRuleCustom.setLowerLimit(this.dyeSdkService.replaceBoundlessTo99(dyeRuleCustom.getTempLowerLimit()));
        boolean result = this.dyeRuleCustomService.save(dyeRuleCustom);
        return result ? ResponseUtil.successResult("保存成功") : ResponseUtil.errorResult("保存失败");
    }

    /**
     * 更新
     */
    @ApiOperation(value = "更新")
    @RequestMapping(value = "updateDyeRuleCustom")
    public ResponseResult updateDyeRuleCustom(@RequestBody DyeRuleCustom dyeRuleCustom) {
        dyeRuleCustom.setUpdateTime(DateUtil.format(new Date(), DateUtil.FORMAT_DATE_YYYY_MM_DD_HH_MM_SS));
        dyeRuleCustom.setUpperLimit(this.dyeSdkService.replaceBoundlessTo99(dyeRuleCustom.getTempUpperLimit()));
        dyeRuleCustom.setLowerLimit(this.dyeSdkService.replaceBoundlessTo99(dyeRuleCustom.getTempLowerLimit()));
        boolean result = this.dyeRuleCustomService.updateById(dyeRuleCustom);
        return result ? ResponseUtil.successResult("更新成功") : ResponseUtil.errorResult("更新失败");
    }

    /**
     * 逻辑删除
     */
    @ApiOperation(value = "逻辑删除")
    @RequestMapping(value = "deleteDyeRuleCustom")
    public ResponseResult deleteDyeRuleCustom(@RequestParam String dyeRuleCustomId, @RequestParam String updateUserId) {
        boolean result = this.dyeRuleCustomService.deleteDyeRuleCustomById(dyeRuleCustomId, updateUserId);
        return result ? ResponseUtil.successResult("删除成功") : ResponseUtil.errorResult("删除失败");
    }

}
