package com.energy.manage.service.service.units.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.energy.manage.common.base.BaseServiceImpl;
import com.energy.manage.common.base.Page;
import com.energy.manage.common.constant.redis.ManagerRedisKeyConstant;
import com.energy.manage.common.enums.DeleteStatusEnum;
import com.energy.manage.common.enums.IdPrefixEnum;
import com.energy.manage.common.po.units.*;
import com.energy.manage.common.util.IdGeneratorUtil;
import com.energy.manage.service.domain.dto.units.*;
import com.energy.manage.service.domain.vo.units.UnitDictBrandModelVo;
import com.energy.manage.service.domain.vo.units.UnitDictConstantsVo;
import com.energy.manage.service.domain.vo.units.UnitDictRoutineListVo;
import com.energy.manage.service.domain.vo.units.UnitVo;
import com.energy.manage.service.mappers.units.*;
import com.energy.manage.service.service.cache.CacheService;
import com.energy.manage.service.service.dict.DictConstantsService;
import com.energy.manage.service.service.units.UnitDictConstantsService;
import com.energy.manage.service.service.units.UnitService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
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 org.springframework.util.CollectionUtils;
import springfox.documentation.schema.Collections;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * 部件字典表接口
 */
@Slf4j
@Service
public class UnitServiceImpl implements UnitService {

    @Autowired
    private UnitDictConstantsService unitDictConstantsService;

    @Autowired
    private UnitBearingsMapper unitBearingsMapper;

    @Autowired
    private UnitDynamoMapper unitDynamoMapper;

    @Autowired
    private UnitGearboxMapper unitGearboxMapper;

    @Autowired
    private UnitGearboxStructureMapper unitGearboxStructureMapper;

    @Autowired
    private UnitGearboxBearingsMapper unitGearboxBearingsMapper;

    @Autowired
    private CacheService cacheService;


    @Override
    public List<UnitDictRoutineListVo> getUnitDictRoutineList(UnitDictRoutineDto unitDictRoutineDto) {
        List<UnitDictConstantsVo> unitDictConstantsVos = unitDictConstantsService.getUnitDictConstantsList(unitDictRoutineDto.getContentsType());
        UnitDictRoutineListVo vo = null;
        List<UnitDictRoutineListVo> listVos = Lists.newArrayList();
        for (UnitDictConstantsVo dictConstantsVo : unitDictConstantsVos) {
            vo = new UnitDictRoutineListVo();
            vo.setKeyId(dictConstantsVo.getContentsValue());
            vo.setValueName(dictConstantsVo.getContentsName());
            listVos.add(vo);
        }
        return listVos;
    }


    @Override
    public List<String> getBrandModelNameOrModelNumber(UnitDictBrandModelDto unitDictBrandModelDto) {
//        if (StringUtils.isEmpty(unitDictBrandModelDto.getManufacture())) {
//            return unitDictConstantsService.getUnitDictBrandModelList(unitDictBrandModelDto);
//        }
        return unitDictConstantsService.getUnitDictBrandModelList(unitDictBrandModelDto);
//        String brandlistKey = ManagerRedisKeyConstant.build(ManagerRedisKeyConstant.IDGENERATOR_UDPB_KEY, unitDictBrandModelDto.getManufacture());
//        String val = cacheService.get(brandlistKey);
//        if (StringUtils.isEmpty(val)) {
//            List<String> valueString = unitDictConstantsService.getUnitDictBrandModelList(unitDictBrandModelDto);
//            log.info("品牌对应型号 ===> " + JSON.toJSONString(valueString));
//            cacheService.add(brandlistKey, JSON.toJSONString(valueString));
//            return valueString;
//        }
//        List<String> modelList = JSONArray.parseArray(val, String.class);
//        return modelList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modificationUnit(UnitDto unitDto, Integer userId) {

        boolean flg = false;
        // 处理主轴
        outBearings:
        if (unitDto.getUnitBearingsDto() != null) {
            UnitBearingsPO unitBearingsPO = new UnitBearingsPO();
            BeanUtil.copyProperties(unitDto.getUnitBearingsDto(), unitBearingsPO);

            //更新
            if (unitBearingsPO.getId() != null && unitBearingsPO.getId() > 0) {
                unitBearingsPO.setUpdateBy(userId);
                unitBearingsPO.setMillTypeCode(unitDto.getMillTypeCode());
                Example queryExample = new Example(UnitBearingsPO.class);
                Example.Criteria criteria = queryExample.createCriteria();
                criteria.andEqualTo("id", unitBearingsPO.getId());
                flg = unitBearingsMapper.updateByExampleSelective(unitBearingsPO, queryExample) > 0;
                break outBearings;
            }
            // 创建
            String unitBearingsCode = IdPrefixEnum.UNIT_BEARINGS_CODE.getCode().concat(IdGeneratorUtil.zeroFillUtil(cacheService.incr(ManagerRedisKeyConstant.build(ManagerRedisKeyConstant.IDGENERATOR_CONSTANTS_KEY, IdPrefixEnum.UNIT_BEARINGS_CODE.getCode()))));
            unitBearingsPO.setMillTypeCode(unitDto.getMillTypeCode());
            unitBearingsPO.setCode(unitBearingsCode);
            unitBearingsPO.setCreateBy(userId);
            unitBearingsPO.setUpdateBy(userId);
            unitBearingsPO.setCreateTime(new Date());
            unitBearingsPO.setUpdateTime(new Date());
            unitBearingsPO.setDelState(DeleteStatusEnum.NODELETE.getCode());
            flg = unitBearingsMapper.insertUseGeneratedKeys(unitBearingsPO) > 0;
        }

        // 处理发电机数据
        outDynamo:
        if (unitDto.getUnitDynamoDto() != null) {
            UnitDynamoPO unitDynamoPO = new UnitDynamoPO();
            BeanUtil.copyProperties(unitDto.getUnitDynamoDto(), unitDynamoPO);

            //更新
            if (unitDynamoPO.getId() != null && unitDynamoPO.getId() > 0) {
                unitDynamoPO.setUpdateBy(userId);
                unitDynamoPO.setMillTypeCode(unitDto.getMillTypeCode());
                Example queryExample = new Example(UnitDynamoPO.class);
                Example.Criteria criteria = queryExample.createCriteria();
                criteria.andEqualTo("id", unitDynamoPO.getId());
                flg = unitDynamoMapper.updateByExampleSelective(unitDynamoPO, queryExample) > 0;
                break outDynamo;
            }
            // 创建
            String unitDynamoCode = IdPrefixEnum.UNIT_DYNAMO_CODE.getCode().concat(IdGeneratorUtil.zeroFillUtil(cacheService.incr(ManagerRedisKeyConstant.build(ManagerRedisKeyConstant.IDGENERATOR_CONSTANTS_KEY, IdPrefixEnum.UNIT_DYNAMO_CODE.getCode()))));
            unitDynamoPO.setMillTypeCode(unitDto.getMillTypeCode());
            unitDynamoPO.setCode(unitDynamoCode);
            unitDynamoPO.setCreateTime(new Date());
            unitDynamoPO.setUpdateTime(new Date());
            unitDynamoPO.setCreateBy(userId);
            unitDynamoPO.setUpdateBy(userId);
            unitDynamoPO.setDelState(DeleteStatusEnum.NODELETE.getCode());
            flg = unitDynamoMapper.insertUseGeneratedKeys(unitDynamoPO) > 0;
        }

        // 处理齿轮箱总表数据
        UnitGearboxPO unitGearboxPO = null;
        outunitGearbox:
        if (unitDto.getUnitGearDto() != null) {
            unitGearboxPO = new UnitGearboxPO();
            unitGearboxPO.setLubricantBrand(unitDto.getUnitGearDto().getLubricantBrand());
            unitGearboxPO.setLubricantModel(unitDto.getUnitGearDto().getLubricantModel());
            unitGearboxPO.setUpdateTime(new Date());
            unitGearboxPO.setUpdateBy(userId);
            //更新齿轮箱总表
            if (unitDto.getUnitGearDto().getId() != null && unitDto.getUnitGearDto().getId() > 0) {
                unitGearboxPO.setId(unitDto.getUnitGearDto().getId());
                unitGearboxPO.setCode(unitDto.getUnitGearDto().getCode());
                Example queryExample = new Example(UnitDynamoPO.class);
                Example.Criteria criteria = queryExample.createCriteria();
                criteria.andEqualTo("id", unitGearboxPO.getId());
                flg = unitGearboxMapper.updateByExampleSelective(unitGearboxPO, queryExample) > 0;
                break outunitGearbox;
            }
            unitGearboxPO.setMillTypeCode(unitDto.getMillTypeCode());
            String unitGearboxCode = IdPrefixEnum.UNIT_GEAR_CODE.getCode().concat(IdGeneratorUtil.zeroFillUtil(cacheService.incr(ManagerRedisKeyConstant.build(ManagerRedisKeyConstant.IDGENERATOR_CONSTANTS_KEY, IdPrefixEnum.UNIT_GEAR_CODE.getCode()))));
            unitGearboxPO.setCode(unitGearboxCode);
            unitGearboxPO.setCreateTime(new Date());
            unitGearboxPO.setCreateBy(userId);
            unitGearboxPO.setDelState(DeleteStatusEnum.NODELETE.getCode());
            flg = unitGearboxMapper.insertUseGeneratedKeys(unitGearboxPO) > 0;

        }

        if (unitGearboxPO != null) {
            // 处理齿轮箱轴承处理
            if (!CollectionUtils.isEmpty(unitDto.getUnitGearDto().getUnitGearboxBearingsDtoList())) {

                UnitGearboxBearingsPO unitGearboxBearingsPO = null;
                List<UnitGearboxBearingsPO> unitGearboxBearingsSaveList = Lists.newArrayList();
                List<UnitGearboxBearingsPO> unitGearboxBearingsUpdateList = Lists.newArrayList();
                for (UnitDto.UnitGearboxBearingsDto dto : unitDto.getUnitGearDto().getUnitGearboxBearingsDtoList()) {
                    unitGearboxBearingsPO = new UnitGearboxBearingsPO();
                    BeanUtil.copyProperties(dto, unitGearboxBearingsPO);
                    if (dto.getId() == null) {
                        unitGearboxBearingsPO.setGearboxCode(unitGearboxPO.getCode());
                        unitGearboxBearingsPO.setCreateBy(userId);
                        unitGearboxBearingsPO.setUpdateBy(userId);
                        unitGearboxBearingsPO.setCreateTime(new Date());
                        unitGearboxBearingsPO.setUpdateTime(new Date());
                        unitGearboxBearingsSaveList.add(unitGearboxBearingsPO);
                    }else{
                        unitGearboxBearingsPO.setUpdateBy(userId);
                        unitGearboxBearingsPO.setUpdateTime(new Date());
                        unitGearboxBearingsUpdateList.add(unitGearboxBearingsPO);
                    }
                }
                if(!CollectionUtils.isEmpty(unitGearboxBearingsSaveList)){
                    unitGearboxBearingsMapper.insertList(unitGearboxBearingsSaveList);
                }
                if(!CollectionUtils.isEmpty(unitGearboxBearingsUpdateList)){
                    unitGearboxBearingsMapper.batchUpdate(unitGearboxBearingsUpdateList);
                }
            }

            // 处理齿轮箱结构
            if (!CollectionUtils.isEmpty(unitDto.getUnitGearDto().getUnitGearboxStructureDtoList())) {
                UnitGearboxStructurePO unitGearboxStructurePO = null;
                List<UnitGearboxStructurePO> unitGearboxStructureSaveList = Lists.newArrayList();
                List<UnitGearboxStructurePO> unitGearboxStructureUpdateList = Lists.newArrayList();
                for (UnitDto.UnitGearboxStructureDto dto : unitDto.getUnitGearDto().getUnitGearboxStructureDtoList()) {
                    unitGearboxStructurePO = new UnitGearboxStructurePO();
                    BeanUtil.copyProperties(dto, unitGearboxStructurePO);
                    if(dto.getId()==null){
                        unitGearboxStructurePO.setGearboxCode(unitGearboxPO.getCode());
                        unitGearboxStructurePO.setCreateTime(new Date());
                        unitGearboxStructurePO.setUpdateTime(new Date());
                        unitGearboxStructurePO.setCreateBy(userId);
                        unitGearboxStructurePO.setUpdateBy(userId);
                        unitGearboxStructureSaveList.add(unitGearboxStructurePO);
                    }else{
                        unitGearboxStructurePO.setUpdateTime(new Date());
                        unitGearboxStructurePO.setUpdateBy(userId);
                        unitGearboxStructureUpdateList.add(unitGearboxStructurePO);
                    }

                }
                if(!CollectionUtils.isEmpty(unitGearboxStructureSaveList)){
                    unitGearboxStructureMapper.insertList(unitGearboxStructureSaveList);
                }
                if(!CollectionUtils.isEmpty(unitGearboxStructureUpdateList)){
                    unitGearboxStructureMapper.batchUpdate(unitGearboxStructureUpdateList);
                }
            }
        }
        return flg;
    }

    @Override
    public UnitVo getUnitVo(String millTypeCode) {

        UnitVo unitVo = new UnitVo();
        unitVo.setMillTypeCode(millTypeCode);

        if (StringUtils.isEmpty(millTypeCode)) {
            return null;
        }

        // 主轴
        Example queryExample = new Example(UnitBearingsPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("millTypeCode", millTypeCode);
        UnitBearingsPO unitBearingsPO = unitBearingsMapper.selectOneByExample(queryExample);

        if (!ObjectUtil.isEmpty(unitBearingsPO)) {
            UnitVo.UnitBearingsVo unitBearingsVo = new UnitVo.UnitBearingsVo();
            BeanUtil.copyProperties(unitBearingsPO, unitBearingsVo);
            unitVo.setUnitBearingsVo(unitBearingsVo);
        }

        // 发电机
        Example queryUdExample = new Example(UnitDynamoPO.class);
        Example.Criteria criteriaUd = queryUdExample.createCriteria();
        criteriaUd.andEqualTo("millTypeCode", millTypeCode);
        UnitDynamoPO unitDynamoPO = unitDynamoMapper.selectOneByExample(queryUdExample);

        if (!ObjectUtil.isEmpty(unitDynamoPO)) {
            UnitVo.UnitDynamoVo unitDynamoVo = new UnitVo.UnitDynamoVo();
            BeanUtil.copyProperties(unitDynamoPO, unitDynamoVo);
            unitVo.setUnitDynamoVo(unitDynamoVo);
        }

        // 齿轮箱
        UnitVo.UnitGearVo unitGearVo = new UnitVo.UnitGearVo();
        Example queryUgExample = new Example(UnitGearboxPO.class);
        Example.Criteria criteriaUg = queryUgExample.createCriteria();
        criteriaUg.andEqualTo("millTypeCode", millTypeCode);

        UnitGearboxPO unitGearboxPO = unitGearboxMapper.selectOneByExample(queryUgExample);
        BeanUtil.copyProperties(unitGearboxPO, unitGearVo);

        // 齿轮箱轴承
        if (!ObjectUtil.isEmpty(unitGearboxPO)) {
            Example queryUgbExample = new Example(UnitGearboxBearingsPO.class);
            Example.Criteria criteriaUgb = queryUgbExample.createCriteria();
            criteriaUgb.andEqualTo("gearboxCode", unitGearboxPO.getCode());
            List<UnitGearboxBearingsPO> unitGearboxBearingsPOList = unitGearboxBearingsMapper.selectByExample(queryUgbExample);

            UnitVo.UnitGearboxBearingsVo unitGearboxBearingsVo = null;
            List<UnitVo.UnitGearboxBearingsVo> unitGearboxBearingsVos = Lists.newArrayList();
            for (UnitGearboxBearingsPO unitGearboxBearingsPO : unitGearboxBearingsPOList) {
                unitGearboxBearingsVo = new UnitVo.UnitGearboxBearingsVo();
                BeanUtil.copyProperties(unitGearboxBearingsPO, unitGearboxBearingsVo);
                unitGearboxBearingsVos.add(unitGearboxBearingsVo);
            }
            unitGearVo.setUnitGearboxBearingsList(unitGearboxBearingsVos);

            // 齿轮箱结构
            Example queryUgsExample = new Example(UnitGearboxStructurePO.class);
            Example.Criteria criteriaUgs = queryUgbExample.createCriteria();
            criteriaUgs.andEqualTo("gearboxCode", unitGearboxPO.getCode());
            List<UnitGearboxStructurePO> unitGearboxStructurePOList = unitGearboxStructureMapper.selectByExample(queryUgbExample);

            UnitVo.UnitGearboxStructureVo unitGearboxStructureVo = null;
            List<UnitVo.UnitGearboxStructureVo> unitGearboxStructureVos = Lists.newArrayList();
            for (UnitGearboxStructurePO unitGearboxStructurePO : unitGearboxStructurePOList) {
                unitGearboxStructureVo = new UnitVo.UnitGearboxStructureVo();
                BeanUtil.copyProperties(unitGearboxStructurePO, unitGearboxStructureVo);
                unitGearboxStructureVos.add(unitGearboxStructureVo);
            }

            unitGearVo.setUnitGearboxStructureList(unitGearboxStructureVos);
            unitVo.setUnitGearVo(unitGearVo);
        }
        return unitVo;
    }

    @Override
    public UnitBearingsPO verifyUnitBearingByMillTypeCode(String millTypeCode) {

        Example queryExample = new Example(UnitBearingsPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("millTypeCode", millTypeCode);

        return unitBearingsMapper.selectOneByExample(queryExample);
    }

    @Override
    public UnitDynamoPO verifyUnitDynamoByMillTypeCode(String millTypeCode) {

        Example queryExample = new Example(UnitDynamoPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("millTypeCode", millTypeCode);

        return unitDynamoMapper.selectOneByExample(queryExample);
    }


}
