package com.mvc.service.taizhang;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.constant.DeviceState;
import com.mvc.dao.TStkoutDetailMapper;
import com.mvc.entity.TStkoutDetail;
import com.mvc.entity.TStkoutDetailExample;
import com.mvc.model.DeviceAccModel;
import com.mvc.model.StateChangeLogModel;
import com.mvc.model.TStkoutDetailModel;
import com.mvc.model.TStkoutModel;
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 TStkoutDetailService {

    @Autowired(required = false)
    private TStkoutDetailMapper tStkoutDetailMapper;

    @Autowired
    private DeviceAccService deviceAccService;

    @Autowired
    private TStkoutService tStkoutService;

    @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);
        TStkoutDetailExample example=new TStkoutDetailExample();
        TStkoutDetailExample.Criteria criteria = example.createCriteria();
        criteria.andBillCodeEqualTo(billCode);
        List<TStkoutDetail> TStkoutDetails = tStkoutDetailMapper.selectByExample(example);
        List<TStkoutDetailModel> resList=new ArrayList<>();
        for(TStkoutDetail TStkoutDetail:TStkoutDetails){
            TStkoutDetailModel model = new TStkoutDetailModel();
            BeanUtils.copyProperties(TStkoutDetail,model);
            resList.add(model);
        }
        resMap.put("total",pageObject.getTotal());
        resMap.put("list",resList);
        return resMap;
    }

    public TStkoutDetailModel saveTStkoutDetail(TStkoutDetailModel model){
        TStkoutDetail tStkoutDetail = new TStkoutDetail();
        BeanUtils.copyProperties(model,tStkoutDetail);
        int insert = tStkoutDetailMapper.insert(tStkoutDetail);
        model.setId(tStkoutDetail.getId());
        return model;
    }

    public List<TStkoutDetailModel> saveTStkoutDetail(String billCode,String devsns) {
        Date nowdate=new Date();
        List<TStkoutDetailModel> resList=new ArrayList<>();
        TStkoutModel TStkoutModel = tStkoutService.findTStkoutByBillCode(billCode);
        String[] split = devsns.split(",");
        for(int i = 0;i<split.length;i++){
            DeviceAccModel devicemodel = deviceAccService.findDeviceAccBysn(split[i]);
            TStkoutDetail TStkoutDetail = new TStkoutDetail();
            TStkoutDetail.setBillCode(billCode);
            TStkoutDetail.setDeatilId(billCode+"_"+split[i]);
            TStkoutDetail.setDeviceType(devicemodel.getDeviceType());
            TStkoutDetail.setDeviceSn(split[i]);

            int insert = tStkoutDetailMapper.insert(TStkoutDetail);
            TStkoutDetailModel resModel=new TStkoutDetailModel();
            BeanUtils.copyProperties(TStkoutDetail,resModel);
            resList.add(resModel);

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

            //更新设备台账设备状态
            devicemodel.setCurStateType(DeviceState.Chuku.getFlag());
            deviceAccService.updateDeviceAcc(devicemodel);
        }
        //更新主表
        TStkoutModel.setStkoutNum(TStkoutModel.getStkoutNum()+split.length);
        tStkoutService.updateTStkout(TStkoutModel);
        return resList;
    }

    public TStkoutDetailModel findTStkoutDetailByCustomId(String  customerId) {
        TStkoutDetailExample example=new TStkoutDetailExample();
        TStkoutDetailExample.Criteria criteria = example.createCriteria();

        List<TStkoutDetail> TStkoutDetails = tStkoutDetailMapper.selectByExample(example);
        if(TStkoutDetails.size()>0){
            TStkoutDetailModel resModel= new TStkoutDetailModel();
            BeanUtils.copyProperties(TStkoutDetails.get(0),resModel);
            return resModel;
        }else{
            return null;
        }

    }

    public TStkoutDetailModel updateTStkoutDetail(TStkoutDetailModel model) {
        TStkoutDetail TStkoutDetail = tStkoutDetailMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model,TStkoutDetail);
        int i = tStkoutDetailMapper.updateByPrimaryKey(TStkoutDetail);
        return model;
    }

    public int deleteTStkoutDetail(String ids) {
        if(StringUtils.isNotEmpty(ids)){
            String[] split = ids.split(",");
            int res=0;
            String billcode="";
            for(int i = 0;i<split.length;i++){
                //更新设备状态
                TStkoutDetail detail = tStkoutDetailMapper.selectByPrimaryKey(Integer.valueOf(split[i]));
                billcode=detail.getBillCode();
                DeviceAccModel deviceAccBysn = deviceAccService.findDeviceAccBysn(detail.getDeviceSn());
                deviceAccBysn.setCurStateType(DeviceState.Ruku.getFlag());
                deviceAccService.updateDeviceAcc(deviceAccBysn);
                res+= tStkoutDetailMapper.deleteByPrimaryKey(Integer.valueOf(split[i]));

                //更新电池为初始状态 2022-11-03 guozhao
                if(StringUtils.isNotEmpty(deviceAccBysn.getBatCode())){
                    batteryAccService.updateBatteryStatus(deviceAccBysn.getBatCode(),DeviceState.Ruku.getFlag());
                }
            }
            //更新入库数量
            //tStkoutService.updateStkoutNum(billcode);
//            TStkoutModel TStkoutByBillCode = TStkoutService.findTStkoutByBillCode(billcode);
//            if(TStkoutByBillCode!=null){
//                TStkoutByBillCode.setStkoutNum(TStkoutByBillCode.getStkoutNum().intValue()-res);
//                TStkoutService.updateTStkout(TStkoutByBillCode);
//            }
            return res;
        }else{
            return 0;
        }
    }

    /**
     * 根据出库单据编号获得明细列表
     * @param billCode
     * @return
     */
    public  List<TStkoutDetailModel> findTStkoutDetailsBybillCode(String billCode) {
        TStkoutDetailExample example=new TStkoutDetailExample();
        TStkoutDetailExample.Criteria criteria = example.createCriteria();
        criteria.andBillCodeEqualTo(billCode);
        List<TStkoutDetail> TStkoutDetails = tStkoutDetailMapper.selectByExample(example);
        List<TStkoutDetailModel> resList=new ArrayList<>();
        for(TStkoutDetail TStkoutDetail:TStkoutDetails){
            TStkoutDetailModel model = new TStkoutDetailModel();
            BeanUtils.copyProperties(TStkoutDetail,model);
            resList.add(model);
        }
        return resList;
    }
}
