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.DyeRuleDefault;
import com.persagy.dye.service.IDyeRuleDefaultService;
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 = "/dyeRuleDefault", method = RequestMethod.POST)
public class DyeRuleDefaultController {

    @Autowired
    private IDyeRuleDefaultService dyeRuleDefaultService;

    @Autowired
    private DyeSdkServiceImpl dyeSdkService;

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

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

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

    /**
     * 物理删除
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "deleteDyeRuleDefault")
    public ResponseResult deleteDyeRuleDefault(@RequestParam String dyeRuleDefaultId, @RequestParam String updateUserId) {
        boolean result = this.dyeRuleDefaultService.deleteDyeRuleDefaultById(dyeRuleDefaultId, updateUserId);
        return result ? ResponseUtil.successResult("删除成功") : ResponseUtil.errorResult("删除失败");
    }

}
