package com.mvc.service.taizhang;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.constant.DeviceState;
import com.mvc.dao.TStkinDetailMapper;
import com.mvc.entity.TStkinDetail;
import com.mvc.entity.TStkinDetailExample;
import com.mvc.model.DeviceAccModel;
import com.mvc.model.StateChangeLogModel;
import com.mvc.model.TStkinDetailModel;
import com.mvc.model.TStkinModel;
import com.mvc.utils.DateUtil;
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
@Transactional
public class TStkinDetailService {

    @Autowired
    private TStkinDetailMapper tStkinDetailMapper;

    @Autowired
    private DeviceAccService deviceAccService;

    @Autowired
    private TStkinService tStkinService;

    @Autowired
    private StateChangeLogService stateChangeLogService;

    @Autowired
    private BatteryAccService batteryAccService;

    public Map<String, Object> findBybillCodeByPage(String billCode, Integer page, Integer rows) {
        Map<String, Object> resMap = new HashMap<>();
        Page<Object> pageObject = PageHelper.startPage(page, rows);
        TStkinDetailExample example = new TStkinDetailExample();
        TStkinDetailExample.Criteria criteria = example.createCriteria();
        criteria.andBillCodeEqualTo(billCode);
        List<TStkinDetail> TStkinDetails = tStkinDetailMapper.selectByExample(example);
        List<TStkinDetailModel> resList = new ArrayList<>();
        for (TStkinDetail TStkinDetail : TStkinDetails) {
            TStkinDetailModel model = new TStkinDetailModel();
            BeanUtils.copyProperties(TStkinDetail, model);
            resList.add(model);
        }
        resMap.put("total", pageObject.getTotal());
        resMap.put("list", resList);
        return resMap;
    }

    public List<TStkinDetailModel> findBybillCode(String billCode) {
        TStkinDetailExample example = new TStkinDetailExample();
        TStkinDetailExample.Criteria criteria = example.createCriteria();
        criteria.andBillCodeEqualTo(billCode);
        List<TStkinDetail> TStkinDetails = tStkinDetailMapper.selectByExample(example);
        List<TStkinDetailModel> resList = new ArrayList<>();
        for (TStkinDetail TStkinDetail : TStkinDetails) {
            TStkinDetailModel model = new TStkinDetailModel();
            BeanUtils.copyProperties(TStkinDetail, model);
            resList.add(model);
        }
        return resList;
    }

    public List<TStkinDetailModel> saveTStkinDetail(String billCode, String devsns) {
        Date nowdate = new Date();
        List<TStkinDetailModel> resList = new ArrayList<>();
        TStkinModel tStkinModel = tStkinService.findTStkinByBillCode(billCode);
        String[] split = devsns.split(",");
        for (int i = 0; i < split.length; i++) {
            DeviceAccModel devicemodel = deviceAccService.findDeviceAccBysn(split[i]);
            TStkinDetail TStkinDetail = new TStkinDetail();
            TStkinDetail.setBillCode(billCode);
            TStkinDetail.setDeatilId(billCode + "_" + split[i]);
            TStkinDetail.setDeviceType(devicemodel.getDeviceType());
            TStkinDetail.setDeviceSn(split[i]);
            TStkinDetail.setStkroomId(tStkinModel.getStkroomId());
            TStkinDetail.setStkroomName(tStkinModel.getStkroomName());
            int insert = tStkinDetailMapper.insert(TStkinDetail);
            TStkinDetailModel resModel = new TStkinDetailModel();
            BeanUtils.copyProperties(TStkinDetail, resModel);
            resList.add(resModel);

            //生成设备状态记录表
            StateChangeLogModel logModel = new StateChangeLogModel();
            logModel.setStateChangeId(DateUtil.formateDate(nowdate, "yyyyMMddHHmmss") + "_" + split[i] + "_" + DeviceState.Ruku.getFlag());
            logModel.setDeviceSn(split[i]);
            logModel.setStateType(DeviceState.Ruku.getFlag());
            logModel.setCreateTime(nowdate);
            stateChangeLogService.saveStateChangeLog(logModel);

            //更新设备状态
            devicemodel.setCurStateType(DeviceState.Ruku.getFlag());
            deviceAccService.updateDeviceAcc(devicemodel);

        }
        //更新主表
        tStkinModel.setStkinNum(tStkinModel.getStkinNum() + split.length);
        tStkinService.updateTStkin(tStkinModel);
        return resList;
    }

    public TStkinDetailModel findTStkinDetailByCustomId(String customerId) {
        TStkinDetailExample example = new TStkinDetailExample();
        TStkinDetailExample.Criteria criteria = example.createCriteria();

        List<TStkinDetail> TStkinDetails = tStkinDetailMapper.selectByExample(example);
        if (TStkinDetails.size() > 0) {
            TStkinDetailModel resModel = new TStkinDetailModel();
            BeanUtils.copyProperties(TStkinDetails.get(0), resModel);
            return resModel;
        } else {
            return null;
        }

    }

    public TStkinDetailModel updateTStkinDetail(TStkinDetailModel model) {
        TStkinDetail TStkinDetail = tStkinDetailMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, TStkinDetail);
        int i = tStkinDetailMapper.updateByPrimaryKey(TStkinDetail);
        return model;
    }

    public int deleteTStkinDetail(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return 0;
        }

        String[] split = ids.split(",");
        int res = 0;
        String billcode = "";
        for (int i = 0; i < split.length; i++) {
            //更新设备状态
            TStkinDetail detail = tStkinDetailMapper.selectByPrimaryKey(Integer.valueOf(split[i]));
  /*          billcode = detail.getBillCode();      //未确认入库前是没有更新状态的
            DeviceAccModel deviceAccBysn = deviceAccService.findDeviceAccBysn(detail.getDeviceSn());
            deviceAccBysn.setBatCode("");
            deviceAccBysn.setCurStateType(DeviceState.Kaoji.getFlag());//??入库前的状态不一定是拷机
            deviceAccService.updateDeviceAcc(deviceAccBysn);*/
            res += tStkinDetailMapper.deleteByPrimaryKey(Integer.valueOf(split[i]));

            //更新电池为初始状态 2022-11-03 guozhao
/*            if(StringUtils.isNotEmpty(detail.getBatCode())){
                batteryAccService.updateBatteryStatus(detail.getBatCode(),"0");
            }*/
        }
        //更新入库数量
        //tStkinService.updateStkInNum(billcode);
//        TStkinModel tStkinByBillCode = tStkinService.findTStkinByBillCode(billcode);
//        if (tStkinByBillCode != null) {
//            tStkinByBillCode.setStkinNum(tStkinByBillCode.getStkinNum().intValue() - res);
//            tStkinService.updateTStkin(tStkinByBillCode);
//        }
        return res;
    }
}
