package com.chinaitop.depot.intelligent.basicdata.service.impl;

import com.chinaitop.depot.intelligent.basicdata.mapper.TWarningThresholdListMapper;
import com.chinaitop.depot.intelligent.basicdata.mapper.TWarningThresholdMapper;
import com.chinaitop.depot.intelligent.basicdata.model.TWarningThreshold;
import com.chinaitop.depot.intelligent.basicdata.model.TWarningThresholdExample;
import com.chinaitop.depot.intelligent.basicdata.model.TWarningThresholdList;
import com.chinaitop.depot.intelligent.basicdata.model.TWarningThresholdListExample;
import com.chinaitop.depot.intelligent.basicdata.service.WarningThresholdService;
import com.chinaitop.depot.intelligent.utils.JsonToObjectUtils;
import com.chinaitop.depot.intelligent.utils.UuidUtils;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
@SuppressWarnings("all")
public class WarningThresholdServiceImpl implements WarningThresholdService {
    @Resource
    private TWarningThresholdMapper tWarningThresholdMapper;

    @Resource
    private TWarningThresholdListMapper tWarningThresholdListMapper;

    @Resource
    private UuidUtils uUidUtils;

    @Override
    public List<TWarningThresholdList> getList(String cfCode) {
        TWarningThresholdListExample thresholdExample = new TWarningThresholdListExample();
        TWarningThresholdListExample.Criteria criteria = thresholdExample.createCriteria();
        if (StringUtils.isNotEmpty(cfCode)) {
            criteria.andVCfCodeEqualTo(cfCode);
        }
        criteria.andDelFlagEqualTo(1);//未删除
        thresholdExample.setOrderByClause("v_cf_code");
        List<TWarningThresholdList> tWarningThresholdLists = tWarningThresholdListMapper.selectByExample(thresholdExample);
        return tWarningThresholdLists;
    }

    @Override
    public List<TWarningThreshold> getByCfCode(String cfCode, Integer wState) {
        TWarningThresholdExample example = new TWarningThresholdExample();
        TWarningThresholdExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(cfCode)) {
            criteria.andVCfCodeEqualTo(cfCode);
        }
        if (null != wState) {
            criteria.andWStateEqualTo(wState);
        }
        criteria.andDelFlagEqualTo(1);//未删除
        example.setOrderByClause("w_module ASC");
        List<TWarningThreshold> tWarningThresholdList = tWarningThresholdMapper.selectByExample(example);
        return tWarningThresholdList;
    }

    /**
     * 保存,修改列表和详情表信息
     *
     * @param thresholdList
     */
    @Override
    public void saveAndUpdate(List<TWarningThreshold> thresholdList) {
        for (TWarningThreshold tWarningThreshold : thresholdList) {
            // 保存详情
            if (StringUtils.isBlank(tWarningThreshold.getId())) {
                tWarningThreshold.setId(uUidUtils.getCodeId(tWarningThreshold.getOrgId(), "t_warning_threshold"));
                tWarningThreshold.setwUpdateTime(new Date());
                tWarningThreshold.setDelFlag(1); //未删除
                tWarningThresholdMapper.insert(tWarningThreshold);
            } else {
                // 更新详情
                tWarningThreshold.setwUpdateTime(new Date());
                tWarningThresholdMapper.updateByPrimaryKeySelective(tWarningThreshold);
            }
        }
        // 保存,修改列表
        TWarningThreshold tWarningThreshold = thresholdList.get(0);
        List<TWarningThresholdList> list = this.getList(tWarningThreshold.getvCfCode());
        TWarningThresholdList tWarningThresholdList = new TWarningThresholdList();
        if (list.size() == 0) {
            // 保存
            tWarningThresholdList.setId(uUidUtils.getCodeId(tWarningThreshold.getOrgId(), "t_warning_threshold"));
            tWarningThresholdList.setOrgId(tWarningThreshold.getOrgId());
            tWarningThresholdList.setvCfCode(tWarningThreshold.getvCfCode());
            tWarningThresholdList.setUpdatePerson(tWarningThreshold.getwUpdatePerson());
            tWarningThresholdList.setUpdateTime(new Date());
            tWarningThresholdList.setDelFlag(1); //未删除
            tWarningThresholdListMapper.insert(tWarningThresholdList);
        } else {
            // 更新
            TWarningThresholdListExample example = new TWarningThresholdListExample();
            TWarningThresholdListExample.Criteria criteria = example.createCriteria();
            if (StringUtils.isNotBlank(tWarningThreshold.getvCfCode()))
                criteria.andVCfCodeEqualTo(tWarningThreshold.getvCfCode());
            tWarningThresholdList.setUpdatePerson(tWarningThreshold.getwUpdatePerson());
            tWarningThresholdList.setUpdateTime(new Date());
            tWarningThresholdListMapper.updateByExampleSelective(tWarningThresholdList, example);
        }
    }

    @Transactional
    @Override
    public int deleteById(String id) {
        return tWarningThresholdMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id删除列表并删除详情数据
     *
     * @param id
     * @param vCfCode
     * @return
     */
    @Transactional
    public int deleteThreshold(String id, String vCfCode, String thresholdSetListJson) {
        // 删除列表
        TWarningThresholdList tWarningThresholdList = (TWarningThresholdList) JsonToObjectUtils.jsonToObject(thresholdSetListJson, TWarningThresholdList.class);
        tWarningThresholdList.setDelFlag(0); //删除
        int i = tWarningThresholdListMapper.updateByPrimaryKeySelective(tWarningThresholdList);

        //int i = tWarningThresholdListMapper.deleteByPrimaryKey(id);
        // 删除详情
        Map<String, String> map = new HashMap<String, String>();
        map.put("vCfCode", tWarningThresholdList.getvCfCode());
        map.put("delFlag", "0");
        //int y = tWarningThresholdMapper.deleteByExample(example);
        int y = tWarningThresholdMapper.updateByMap(map);
        if (i > 0 && y > 0) {
            return 1;
        }
        return 0;
    }

    @Override
    public TWarningThreshold getByCfCodeAndwName(String cfCode, Integer wState, String wModule, String wName) {
        TWarningThresholdExample example = new TWarningThresholdExample();
        TWarningThresholdExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(cfCode)) {
            criteria.andVCfCodeEqualTo(cfCode);
        }
        if (null != wState) {
            criteria.andWStateEqualTo(wState);
        }
        if (StringUtils.isNotBlank(wName)) {
            criteria.andWNameEqualTo(wName);
        }
        if (StringUtils.isNotBlank(wModule)) {
            criteria.andWModuleEqualTo(wModule);
        }
        List<TWarningThreshold> tWarningThresholdList = tWarningThresholdMapper.selectByExample(example);
        if (tWarningThresholdList.size() > 0) {
            return tWarningThresholdList.get(0);
        }
        return null;
    }

    @Override
    public HashMap<String, String> getByOrgIdAndCfCode(String orgId, String cfCode) {
        TWarningThresholdExample example = new TWarningThresholdExample();
        TWarningThresholdExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(cfCode)) {
            criteria.andVCfCodeEqualTo(cfCode);
        }
        if (StringUtils.isNotBlank(orgId)) {
            criteria.andOrgIdEqualTo(orgId);
        }
        criteria.andWModuleEqualTo("6972");//测温预警
        criteria.andWStateEqualTo(0);
        criteria.andDelFlagEqualTo(1);//未删除
        example.setOrderByClause("w_name ASC");
        List<TWarningThreshold> tWarningThresholdList = tWarningThresholdMapper.selectByExample(example);
        HashMap<String, String> map = Maps.newHashMap();
        BigDecimal high = null;
        BigDecimal low = null;
        if (tWarningThresholdList.size() > 0) {
            for (int i = 0; i < tWarningThresholdList.size(); i++) {
                TWarningThreshold tWarningThreshold = tWarningThresholdList.get(i);
                String wName = tWarningThreshold.getwName();
                BigDecimal threshold = tWarningThreshold.getThreshold();
                if (wName != null && wName.equals("6983")) {
                    map.put("high", threshold.toString());
                    high = threshold;
                }
                if (wName != null && wName.equals("6982")) {
                    map.put("low", threshold.toString());
                    low = threshold;
                }
                if (wName != null && wName.equals("6991")) {
                    if (high != null) {
                        String whigh = high.subtract(threshold).toString();
                        map.put("whigh", whigh);
                    }
                    if (low != null) {
                        String wlow = low.add(threshold).toString();
                        map.put("wlow", wlow);
                    }
                }
            }
        }
        return map;
    }

    /**
     * 根据orgId或仓房code查询测温预警,报警阀值.
     * @param orgId
     * @param vCfCode
     * @return
     */
    @Override
    public Map<String, Map<String, Object>> getValueByOrgIdOrCfCode(String orgId, String vCfCode) {
        // 查询对象
        TWarningThresholdExample example = new TWarningThresholdExample();
        TWarningThresholdExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(vCfCode)) {
            criteria.andVCfCodeEqualTo(vCfCode);
        }
        if (StringUtils.isNotBlank(orgId)) {
            criteria.andOrgIdEqualTo(orgId);
        }
        criteria.andWModuleEqualTo("6972");//测温预警
        criteria.andWStateEqualTo(0);
        criteria.andDelFlagEqualTo(1);//未删除
        example.setOrderByClause("w_name ASC");
        List<TWarningThreshold> tWarningThresholdList = tWarningThresholdMapper.selectByExample(example);
        // 初始化返回值list对象
        Map<String, Map<String, Object>> mapObj = new HashMap<>();
        if (tWarningThresholdList.size() > 0) {
            Map<String, List<TWarningThreshold>> collect = tWarningThresholdList.stream().collect(Collectors.groupingBy(t -> t.getvCfCode()));
            for (String key : collect.keySet()) {
                Map<String, Object> map = new HashMap<>();
                List<TWarningThreshold> tWarningThresholds = collect.get(key);
                for (TWarningThreshold wt : tWarningThresholds) {
                    if (wt.getwName() != null) {
                        // 报警值
                        if (wt.getwName().equals("6983")) {
                            //高温报警阀值
                            map.put("high", wt.getThreshold());
                        } else if (wt.getwName().equals("6982")) {
                            //低温报警阀值
                            map.put("low", wt.getThreshold());
                        } else if (wt.getwName().equals("6991")) {
                            //高温预警值
                            map.put("whigh", wt.getThreshold());
                        } else if (wt.getwName().equals("7248")) {
                            //低温预警值
                            map.put("wlow", wt.getThreshold());
                        }
                    }
                }
                mapObj.put(key, map);
            }
        }
        return mapObj;
    }

}
