package com.mvc.service.taizhang;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.dao.BatteryAccMapper;
import com.mvc.entity.BatteryAcc;
import com.mvc.entity.BatteryAccExample;
import com.mvc.model.BatteryAccModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

//电池台账Service
@Service
@Transactional
public class BatteryAccService {

    @Autowired(required = false)
    private BatteryAccMapper batteryAccMapper;

    public Map<String, Object> findAllBatteryAcc(BatteryAccModel queryModel) {
        BatteryAccExample example = new BatteryAccExample();
        BatteryAccExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(queryModel.getBatCode())) {
            String code = queryModel.getBatCode().toUpperCase().replace("BAT", "");
            criteria.andBatCodeLike("%" + code + "%");
        }

        Map<String, Object> resMap = new HashMap<>();
        Page<Object> pageObject = PageHelper.startPage(queryModel.getPage(), queryModel.getRows());
        List<BatteryAcc> BatteryAccs = batteryAccMapper.selectByExample(example);
        List<BatteryAccModel> resList = new ArrayList<>();
        for (BatteryAcc BatteryAcc : BatteryAccs) {
            BatteryAccModel model = new BatteryAccModel();
            BeanUtils.copyProperties(BatteryAcc, model);
            resList.add(model);
        }
        resMap.put("total", pageObject.getTotal());
        resMap.put("list", resList);
        return resMap;
    }

    public BatteryAccModel saveBatteryAcc(BatteryAccModel model) {
        if (model.getAddNum() != null && model.getAddNum() > 0) {
            return saveBatterAccBatch(model);
        }
        String[] arrCode = model.getBatCode().split(",");
        if (arrCode != null) {
            for (String code : arrCode) {
                String temp = code.toUpperCase().replace("BAT", "");
                if (findBatteryAccByBatCode(temp) != null) {//电池编号已经存在,跳过
                    continue;
                }
                if (StringUtils.isEmpty(temp)) continue;

                BatteryAcc acc = new BatteryAcc();
                BeanUtils.copyProperties(model, acc);
                acc.setBatCode(temp);
                acc.setCreateMan(model.getCreateMan());
                acc.setCreateTime(new Date());
                acc.setBatStatus("0");
                acc.setStatusUpdateTime(new Date());
                batteryAccMapper.insert(acc);
            }
        }
        return model;
    }

    /**
     * 批量生成电池台账
     *
     * @param model
     * @return
     */
    private BatteryAccModel saveBatterAccBatch(BatteryAccModel model) {
        long code = Long.parseLong(model.getBatCode());
        Date now = new Date();
        for (int i = 0; i < model.getAddNum(); i++) {
            BatteryAcc acc = new BatteryAcc();
            BeanUtils.copyProperties(model, acc);
            acc.setBatCode(String.valueOf(code + i));
            acc.setCreateMan(model.getCreateMan());
            acc.setCreateTime(now);
            acc.setBatStatus("0");
            acc.setStatusUpdateTime(now);
            batteryAccMapper.insert(acc);
        }
        return new BatteryAccModel();
    }

    /**
     * 根据电池编码获取对象
     *
     * @param batCode
     * @return
     */
    public BatteryAccModel findBatteryAccByBatCode(String batCode) {
        batCode = batCode.toUpperCase().replace("BAT", "");
        BatteryAccExample example = new BatteryAccExample();
        BatteryAccExample.Criteria criteria = example.createCriteria();
        criteria.andBatCodeEqualTo(batCode);
        List<BatteryAcc> batteryAccs = batteryAccMapper.selectByExample(example);
        if (batteryAccs.size() > 0) {
            BatteryAccModel resModel = new BatteryAccModel();
            BeanUtils.copyProperties(batteryAccs.get(0), resModel);
            return resModel;
        } else {
            return null;
        }
    }

    public BatteryAccModel updateBatteryAcc(BatteryAccModel model) {
        model.setBatCode(model.getBatCode().toUpperCase().replace("BAT", ""));
        BatteryAcc BatteryAcc = batteryAccMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, BatteryAcc);
        int i = batteryAccMapper.updateByPrimaryKey(BatteryAcc);
        return model;
    }

    public int deleteBatteryAcc(int id) {
        int i = batteryAccMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * 根据电池编号更新电池状态
     *
     * @param batCode
     * @param status
     * @return
     */
    public BatteryAccModel updateBatteryStatus(String batCode, String status) {
        if (StringUtils.isEmpty(batCode) || StringUtils.isEmpty(status)) return null;
        BatteryAccExample example = new BatteryAccExample();
        BatteryAccExample.Criteria criteria = example.createCriteria();
        criteria.andBatCodeEqualTo(batCode);
        List<BatteryAcc> batteryAccs = batteryAccMapper.selectByExample(example);
        if (batteryAccs != null && batteryAccs.size() > 0) {
            BatteryAcc acc = batteryAccs.get(0);
            acc.setBatStatus(status);
            acc.setStatusUpdateTime(new Date());
            if (batteryAccMapper.updateByPrimaryKey(acc) > 0) {
                BatteryAccModel model = new BatteryAccModel();
                BeanUtils.copyProperties(acc, model);
                return model;
            }
        }

        return null;
    }

    /**
     * 更新电池运行记录(运行时长和次数)
     *
     * @param batCode 电池编号
     * @param runTime 运行总时长
     * @param runNum  运行总次数
     * @return
     */
    public BatteryAccModel updateOperaRecord(String batCode, int runTime, int runNum) {
        if (StringUtils.isEmpty(batCode)) return null;
        BatteryAccExample example = new BatteryAccExample();
        BatteryAccExample.Criteria criteria = example.createCriteria();
        criteria.andBatCodeEqualTo(batCode);
        List<BatteryAcc> batteryAccs = batteryAccMapper.selectByExample(example);
        if (batteryAccs != null && batteryAccs.size() > 0) {
            BatteryAcc acc = batteryAccs.get(0);
            acc.setBatRumTime(runTime);
            acc.setBatRunNum(runNum);
            batteryAccMapper.updateByPrimaryKey(acc);
            BatteryAccModel model = new BatteryAccModel();
            BeanUtils.copyProperties(acc, model);
        }

        return null;
    }

    public String checkDataLegitimacy(BatteryAccModel model) {
        if (StringUtils.isEmpty(model.getBatCode())) {
            return "请输入有效的电池编号";
        }

        if (model.getAddNum() == null || model.getAddNum() == 0) {//扫码添加
            String[] arr = model.getBatCode().toUpperCase().replace("BAT", "").replace('，', ',').split(",");
            String errLenMsg = "";
            String errExistMsg = "";
            for (String code : arr) {
                if (code.length() != 15) {
                    errLenMsg += (code + ",");
                }
                if (findBatteryAccByBatCode(code) != null) {
                    errExistMsg += (code + ",");
                }
            }
            if (StringUtils.isNotEmpty(errLenMsg)) {
                return "电池编号长度错误:" + errLenMsg;
            }

            if (StringUtils.isNotEmpty(errExistMsg)) {
                return "电池编号已经存在:" + errLenMsg;
            }
        } else {//批量配置校验有点麻烦
            if (!StringUtils.isNumeric(model.getBatCode())) {
                return "电池编号只能是纯数字";
            }

            long lngCode = Long.parseLong(model.getBatCode());
            String errLenMsg = "";
            String errExistMsg = "";
            for (int i = 0; i < model.getAddNum(); i++) {
                String sCode = String.valueOf(lngCode + i);

                if (sCode.length() != 15) {
                    errLenMsg += (sCode + ",");
                }
                if (findBatteryAccByBatCode(sCode) != null) {
                    errExistMsg += (sCode + ",");
                }
            }

            if (StringUtils.isNotEmpty(errLenMsg)) {
                return "电池编号长度错误:" + errLenMsg;
            }

            if (StringUtils.isNotEmpty(errExistMsg)) {
                return "电池台账已经存在:" + errLenMsg;
            }
        }
        return null;
    }
}
