package com.tykj.dev.union;

import com.tykj.dev.device.allot.repository.AllotBackBillDao;
import com.tykj.dev.device.allot.repository.AllotBillDao;
import com.tykj.dev.device.allot.subject.domin.AllotBackBill;
import com.tykj.dev.device.allot.subject.domin.AllotBill;
import com.tykj.dev.device.confirmcheck.entity.domain.DeviceCheckDetail;
import com.tykj.dev.device.confirmcheck.repository.DeviceCheckDetailDao;
import com.tykj.dev.device.matching.repository.MatchingDeviceLibraryDao;
import com.tykj.dev.device.matching.subject.domin.MatchingDeviceLibrary;
import com.tykj.dev.device.repair.repository.RepairBackBillDao;
import com.tykj.dev.device.repair.repository.RepairBillDao;
import com.tykj.dev.device.repair.repository.RepairDetailDao;
import com.tykj.dev.device.repair.repository.RepairSendBillDao;
import com.tykj.dev.device.repair.subject.domin.RepairBackBill;
import com.tykj.dev.device.repair.subject.domin.RepairBill;
import com.tykj.dev.device.repair.subject.domin.RepairDetail;
import com.tykj.dev.device.repair.subject.domin.RepairSendBill;
import com.tykj.dev.device.selfcheck.repository.SelfCheckBillDao;
import com.tykj.dev.device.selfcheck.subject.domin.SelfCheckBill;
import com.tykj.dev.device.user.subject.dao.UnitsDao;
import com.tykj.dev.device.user.subject.entity.Units;
import com.tykj.dev.device.usereport.repository.DeviceUseReportDao;
import com.tykj.dev.device.usereport.subject.domin.DeviceUseReport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Slf4j
@Component
public class DataInit  {

    @Resource
    private UnitsDao unitsDao;

    @Autowired
    private AllotBillDao allotBillDao;
    @Autowired
    private AllotBackBillDao allotBackBillDao;
    @Autowired
    private RepairBackBillDao repairBackBillDao;
    @Autowired
    private RepairDetailDao repairDetailDao;
    @Autowired
    private RepairSendBillDao repairSendBillDao;
    @Autowired
    private RepairBillDao repairBillDao;
    @Autowired
    private SelfCheckBillDao selfCheckBillDao;
    @Autowired
    private DeviceUseReportDao deviceUseReportDao;

    @Autowired
    private MatchingDeviceLibraryDao matchingDeviceLibraryDao;

    private void initCheckDetailUnitName() {
        // 对现有的数据补充unitId信息
        allotBillDao.findAll().forEach(this::saveModifyDetail1);
        allotBackBillDao.findAll().forEach(this::saveModifyDetail2);
        selfCheckBillDao.findAll().forEach(this::saveModifyDetail3);
        repairBillDao.findAll().forEach(this::saveModifyDetail4);
        repairSendBillDao.findAll().forEach(this::saveModifyDetail5);
        repairDetailDao.findAll().forEach(this::saveModifyDetail6);
        repairBackBillDao.findAll().forEach(this::saveModifyDetail7);
        deviceUseReportDao.findAll().forEach(this::saveModifyDetail8);
        matchingDeviceLibraryDao.findAll().forEach(this::saveModifyDetail9);
    }


    private void saveModifyDetail1(AllotBill allotBill) {
        String receiveUnit = allotBill.getReceiveUnit();
        String sendUnit = allotBill.getSendUnit();

        Integer receiveUnitId = getUnitId(receiveUnit);
        Integer sendUnitId = getUnitId(sendUnit);

        if (receiveUnitId != -1 && sendUnitId != -1) {
            allotBill.setReceiveUnitId(receiveUnitId);
            allotBill.setSendUnitId(sendUnitId);
            log.info("[配发] 修正allotBill历史数据,修正的发件单位:{},修正的接收单位:{},发送单位id:{},接收单位id:{}",
                    sendUnit, receiveUnit,sendUnitId,receiveUnitId);
            allotBillDao.save(allotBill);
        }

    }

    private void saveModifyDetail2(AllotBackBill allotBackBill) {
        String receiveUnit = allotBackBill.getReceiveUnit();
        String sendUnit = allotBackBill.getSendUnit();

        Integer receiveUnitId = getUnitId(receiveUnit);
        Integer sendUnitId = getUnitId(sendUnit);

        if (receiveUnitId != -1 && sendUnitId != -1) {
            allotBackBill.setReceiveUnitId(receiveUnitId);
            allotBackBill.setSendUnitId(sendUnitId);
            log.info("[退回] 修正allotBill历史数据,修正的发件单位:{},修正的接收单位:{},发送单位id:{},接收单位id:{}",
                    sendUnit, receiveUnit,sendUnitId,receiveUnitId);
            allotBackBillDao.save(allotBackBill);
        }

    }

    private void saveModifyDetail3(SelfCheckBill selfCheckBill) {
        String receiveUnit = selfCheckBill.getCheckUnit();
        Integer receiveUnitId = getUnitId(receiveUnit);
        if (receiveUnitId != -1) {
            selfCheckBill.setCheckUnitId(receiveUnitId);
            log.info("[自查] 修正selfCheckBill历史数据,修正的单位:{},接收单位id:{}",
                     receiveUnit,receiveUnitId);
            selfCheckBillDao.save(selfCheckBill);
        }

    }

    private void saveModifyDetail4(RepairBill allotBill) {
        String receiveUnit = allotBill.getReceiveUnit();
        String sendUnit = allotBill.getSendUnit();

        Integer receiveUnitId = getUnitId(receiveUnit);
        Integer sendUnitId = getUnitId(sendUnit);

        if (receiveUnitId != -1 && sendUnitId != -1) {
            allotBill.setReceiveUnitId(receiveUnitId);
            allotBill.setSendUnitId(sendUnitId);
            log.info("[维修] 修正RepairBill历史数据,修正的发件单位:{},修正的接收单位:{},发送单位id:{},接收单位id:{}",
                    sendUnit, receiveUnit,sendUnitId,receiveUnitId);
            repairBillDao.save(allotBill);
        }

    }

    private void saveModifyDetail5(RepairSendBill allotBill) {
        String receiveUnit = allotBill.getReceiveUnit();
        String sendUnit = allotBill.getSendUnit();

        Integer receiveUnitId = getUnitId(receiveUnit);
        Integer sendUnitId = getUnitId(sendUnit);

        if (receiveUnitId != -1 && sendUnitId != -1) {
            allotBill.setReceiveUnitId(receiveUnitId);
            allotBill.setSendUnitId(sendUnitId);
            log.info("[送修] 修正RepairSendBill历史数据,修正的发件单位:{},修正的接收单位:{},发送单位id:{},接收单位id:{}",
                    sendUnit, receiveUnit,sendUnitId,receiveUnitId);
            repairSendBillDao.save(allotBill);
        }

    }

    private void saveModifyDetail6(RepairDetail allotBill) {
        String receiveUnit = allotBill.getLocationUnit();
        String sendUnit = allotBill.getOwnUnit();

        Integer receiveUnitId = getUnitId(receiveUnit);
        Integer sendUnitId = getUnitId(sendUnit);

        if (receiveUnitId != -1 && sendUnitId != -1) {
            allotBill.setLocationUnitId(receiveUnitId);
            allotBill.setOwnUnitId(sendUnitId);
            log.info("[维修详情] 修正RepairDetail历史数据,修正的发件单位:{},修正的接收单位:{},发送单位id:{},接收单位id:{}",
                    sendUnit, receiveUnit,sendUnitId,receiveUnitId);
            repairDetailDao.save(allotBill);
        }

    }
    private void saveModifyDetail7(RepairBackBill allotBill) {
        String receiveUnit = allotBill.getReceiveUnit();
        String sendUnit = allotBill.getSendUnit();

        Integer receiveUnitId = getUnitId(receiveUnit);
        Integer sendUnitId = getUnitId(sendUnit);

        if (receiveUnitId != -1 && sendUnitId != -1) {
            allotBill.setReceiveUnitId(receiveUnitId);
            allotBill.setSendUnitId(sendUnitId);
            log.info("[维修退回] 修正RepairBackBill历史数据,修正的发件单位:{},修正的接收单位:{},发送单位id:{},接收单位id:{}",
                    sendUnit, receiveUnit,sendUnitId,receiveUnitId);
            repairBackBillDao.save(allotBill);
        }

    }

    private void saveModifyDetail8(DeviceUseReport allotBill) {
        String receiveUnit = allotBill.getUnit();
        Integer receiveUnitId = getUnitId(receiveUnit);
        if (receiveUnitId != -1 ) {
            allotBill.setUnitId(receiveUnitId);
            log.info("[用户使用报告] 修正DeviceUseReport历史数据,修正的单位:{},接收单位id:{}",
                    receiveUnit,receiveUnitId);
            deviceUseReportDao.save(allotBill);
        }

    }

    private void saveModifyDetail9(MatchingDeviceLibrary matchingDeviceLibrary) {
        String createUnit = matchingDeviceLibrary.getCreateUnit();
        Integer createUnitId = getUnitId(createUnit);
        if (createUnitId != -1 ) {
            matchingDeviceLibrary.setCreateUnitId(createUnitId);
            log.info("[配套设备库] 修正MatchingDeviceLibrary历史数据,修正的单位:{},接收单位id:{}",
                    createUnit,createUnitId);
            matchingDeviceLibraryDao.save(matchingDeviceLibrary);
        }

    }

    private Integer getUnitId(String unitName) {
        List<Units> units = unitsDao.findAllByName(unitName).orElse(Collections.emptyList());
        return units.size() == 1 ? units.get(0).getUnitId() : -1;
    }

//    @Bean
//    @DependsOn("userUtils")
    public void init2(){
        log.info("单位名称初始化-----");
        initCheckDetailUnitName();
        initCheckDetailUnitName2();
    }

    @Autowired
    private DeviceCheckDetailDao
    deviceCheckDetailDao;

    private void initCheckDetailUnitName2() {
        // 对现有的数据补充unitId信息
        deviceCheckDetailDao.findAll().forEach(this::saveModifyDetail);
    }

    private void saveModifyDetail(DeviceCheckDetail deviceCheckDetail) {
        String checkUnit = deviceCheckDetail.getCheckUnit();
        Integer unitId = getUnitId(checkUnit);
        if (unitId != -1) {
            deviceCheckDetail.setCheckUnitId(unitId);
            log.info("[核查] 修正detail历史数据，修正的单位:{},单位id:{}", checkUnit, unitId);
            deviceCheckDetailDao.save(deviceCheckDetail);
        }
    }
//    @Override
//    @Order(value = 1)
//    public void run(String... args) throws Exception {
//        log.info("单位名称初始化-----");
//        initCheckDetailUnitName();
//    }
}
