package com.njcn.device.pq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.biz.enums.DeviceResponseEnum;
import com.njcn.device.pq.mapper.FlowMealMapper;
import com.njcn.device.pq.mapper.RateOfFlowMapper;
import com.njcn.device.pq.pojo.param.FlowMealParam;
import com.njcn.device.pq.pojo.po.FlowMeal;
import com.njcn.device.pq.pojo.vo.RateOfFlowVO;
import com.njcn.device.pq.service.FlowMealService;
import com.njcn.web.utils.RequestUtil;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @version 1.0.0
 * @author: chenchao
 * @date: 2022/05/11 14:30
 */
@Service
@AllArgsConstructor
public class FlowMealServiceImpl extends ServiceImpl<FlowMealMapper, FlowMeal> implements FlowMealService {

    private final RateOfFlowMapper rateOfFlowMapper;

    @Override
    public List<RateOfFlowVO> getRateOfFlowData() {
        List<RateOfFlowVO> rateOfFlowData = rateOfFlowMapper.getRateOfFlowData();
        return rateOfFlowData;
    }

    @Override
    public boolean addRateOfFlowData(FlowMealParam flowMealParam) {
        checkCode(flowMealParam, false);
        FlowMeal flowMeal = new FlowMeal();
        BeanUtils.copyProperties(flowMealParam, flowMeal);
        flowMeal.setState(DataStateEnum.ENABLE.getCode());
        flowMeal.setCreateBy(RequestUtil.getUserIndex());
        flowMeal.setUpdateBy(RequestUtil.getUserIndex());
        flowMeal.setCreateTime(LocalDateTime.now());
        flowMeal.setUpdateTime(LocalDateTime.now());
        this.save(flowMeal);
        return true;
    }

    @Override
    public boolean updateRateOfFlowData(FlowMealParam.FlowMealUpdateParam flowMealUpdateParam) {
        if (flowMealUpdateParam.getType() == 0) {
            checkCode(flowMealUpdateParam, true);
        }
        FlowMeal flowMeal = new FlowMeal();
        BeanUtils.copyProperties(flowMealUpdateParam, flowMeal);
        flowMeal.setUpdateBy(RequestUtil.getUserIndex());
        flowMeal.setUpdateTime(LocalDateTime.now());
        boolean b = this.updateById(flowMeal);
        if (b) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean deleteRateOfFlowData(String id) {
        rateOfFlowMapper.updateByState(id);
        return true;
    }

    /**
     * 校验参数，检查是否存在相同编码的字典类型
     */
    private void checkCode(FlowMealParam flowMealParam, boolean isExcludeSelf) {
        LambdaQueryWrapper<FlowMeal> nodeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        nodeLambdaQueryWrapper
                .eq(FlowMeal::getName, flowMealParam.getName())
                .eq(FlowMeal::getState, DataStateEnum.ENABLE.getCode());
        //更新的时候，需排除当前记录
        if (isExcludeSelf) {
            if (flowMealParam instanceof FlowMealParam.FlowMealUpdateParam) {
                nodeLambdaQueryWrapper.ne(FlowMeal::getId, ((FlowMealParam.FlowMealUpdateParam) flowMealParam).getId());
            }
        }
        int countByAccount = this.count(nodeLambdaQueryWrapper);
        //大于等于1个则表示重复
        if (countByAccount >= 1) {
            throw new BusinessException(DeviceResponseEnum.FLOW_SAME_NAME);
        }
        List<RateOfFlowVO> rateOfFlowFlag = rateOfFlowMapper.getRateOfFlowFlag();
        if (!CollectionUtils.isEmpty(rateOfFlowFlag)) {
            if (flowMealParam.getFlag().equals(DataStateEnum.ENABLE.getCode())) {
                if (Objects.equals(((FlowMealParam.FlowMealUpdateParam) flowMealParam).getId(), rateOfFlowFlag.get(0).getId())) {
                    if (Objects.equals(flowMealParam.getName(), rateOfFlowFlag.get(0).getName()) && Objects.equals(flowMealParam.getFlow(), rateOfFlowFlag.get(0).getFlow())) {
                        throw new BusinessException(DeviceResponseEnum.FLOW_FLAG);
                    }
                } else if (!Objects.equals(((FlowMealParam.FlowMealUpdateParam) flowMealParam).getId(), rateOfFlowFlag.get(0).getId())) {
                    throw new BusinessException(DeviceResponseEnum.FLOW_FLAG);
                }
            } else {
                if (Objects.equals(((FlowMealParam.FlowMealUpdateParam) flowMealParam).getId(), rateOfFlowFlag.get(0).getId())) {
                    throw new BusinessException(DeviceResponseEnum.FLOW_UPDATE);
                }
            }
        }
    }
}
