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

import com.chinaitop.depot.intelligent.basicdata.model.TKtinfo;
import com.chinaitop.depot.intelligent.basicdata.service.TKtinfoService;
import com.chinaitop.depot.intelligent.coldStorage.mapper.TKtinfoHistoryMapper;
import com.chinaitop.depot.intelligent.coldStorage.model.TKtinfoHistory;
import com.chinaitop.depot.intelligent.coldStorage.model.TKtinfoHistoryExample;
import com.chinaitop.depot.intelligent.coldStorage.service.ColdStorageService;
import com.chinaitop.depot.intelligent.utils.JsonToObjectUtils;
import com.chinaitop.depot.intelligent.utils.UuidUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author qingsong.han
 * @description: 低温储粮
 * @create 2019-01-25 10:52
 */
@Service
public class ColdStorageServiceImpl implements ColdStorageService {
    @Autowired
    private TKtinfoHistoryMapper ktinfoHistoryMapper;
    @Resource
    private TKtinfoService tKtinfoService;

    /**
     * 空调作业记录条件列表
     * @param orgId
     * @param vcfcode
     * @return
     */
    @Override
    public List<TKtinfoHistory> getList(String orgId, String vcfcode) {
        TKtinfoHistoryExample example = new TKtinfoHistoryExample();
        TKtinfoHistoryExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(orgId))
            criteria.andOrgIdEqualTo(orgId);
        if (StringUtils.isNotBlank(vcfcode))
            criteria.andVcfcodeEqualTo(vcfcode);
        criteria.andEnergyIsNull();
        example.setOrderByClause("starttime DESC");
        return ktinfoHistoryMapper.selectByExample(example);
    }

    /**
     * 保存空调作业记录
     * @param type
     * @param ktinfoJson
     * @param uperson
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveHistoryData(String type, String ktinfoJson, String uperson) {
        TKtinfo tKtinfo = (TKtinfo) JsonToObjectUtils.jsonToObject(ktinfoJson, TKtinfo.class);
        TKtinfoHistory ktinfoHistory = new TKtinfoHistory();
        if ("open".equals(type)) {
            ktinfoHistory.setId(UuidUtils.getCode()); // id
            ktinfoHistory.setVcfcode(tKtinfo.getVcfcode()); // 仓房code
            ktinfoHistory.setVdevcode(tKtinfo.getVdevcode()); // 设备code
            ktinfoHistory.setVdevname(tKtinfo.getVdevname()); // 设备名称
            ktinfoHistory.setCurrentmode(tKtinfo.getCurrentmode()); // 空调运行模式
            ktinfoHistory.setUperson(uperson); // 操作人
            ktinfoHistory.setRemark(tKtinfo.getRemark()); // 具体详细信息
            ktinfoHistory.setOrgId(tKtinfo.getOrgId()); // 组织机构代码
            ktinfoHistory.setStarttime(new Date()); // 开始时间
            ktinfoHistoryMapper.insertSelective(ktinfoHistory); // 新增作业记录
            tKtinfoService.save(ktinfoJson); // 更新操作状态
        } else if ("close".equals(type)){
            TKtinfoHistoryExample example = new TKtinfoHistoryExample();
            TKtinfoHistoryExample.Criteria criteria = example.createCriteria();
            criteria.andOrgIdEqualTo(tKtinfo.getOrgId());
            criteria.andVcfcodeEqualTo(tKtinfo.getVcfcode());
            criteria.andVdevcodeEqualTo(tKtinfo.getVdevcode());
            criteria.andVdevnameEqualTo(tKtinfo.getVdevname());
            criteria.andEndtimeIsNull();
            ktinfoHistory.setEndtime(new Date()); // 结束时间
            ktinfoHistoryMapper.updateByExampleSelective(ktinfoHistory,example); // 更新作业记录
            tKtinfoService.save(ktinfoJson); // 更新操作状态
        }
    }

}
