package com.jisco.dynamicweight.pika.service.impl;

import cn.hutool.core.lang.Assert;
import com.jisco.dynamicweight.dynamicworkauditdata.model.DynamicWorkAuditdata;
import com.jisco.dynamicweight.pika.mapper.CenternetMapper;
import com.jisco.dynamicweight.pika.model.Centernet;
import com.jisco.dynamicweight.pika.service.ICenternetService;
import com.steerinfo.framework.datasource.DataSourceKey;
import com.steerinfo.framework.datasource.TargetDataSource;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Centernet服务实现：
 *
 * @author generator
 * @version 1.0-SNAPSHORT 2022-02-11 11:33
 * 类描述
 * 修订历史：
 * 日期：2022-02-11
 * 作者：generator
 * 参考：
 * 描述：Centernet服务实现
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 * @see null
 */
@Service(value = "centernetService")
public class CenternetServiceImpl extends BaseServiceImpl<Centernet, String> implements ICenternetService {

    @Autowired
    private CenternetMapper centernetMapper;

    @Override
    protected IBaseMapper<Centernet, String> getMapper() {
        return centernetMapper;
    }

    @Override
//    @TargetDataSource(dataSourceKey = DataSourceKey.DB_SLAVE2)
    public void upload(List<DynamicWorkAuditdata> list) {
        List<Centernet> centernets = new ArrayList<>();
        list.forEach(item -> {
            Centernet centernet = new Centernet();
            centernet.setIdentity(item.getPikaDataId());
            centernet.setMeasurebill(null);
            centernet.setTrustbillnum(null);
            centernet.setVoucheridentity(null);
            centernet.setVoucheritem(null);
            centernet.setBusinessgroup(null);
            centernet.setBusinesstype("集中计量");
            centernet.setStationtype("BeltScale");
            centernet.setMeasureobjecttype("MaterialMeasure");
            centernet.setConveyance(item.getStationCode());
            centernet.setTrailer(null);
            centernet.setTempconveyance(null);
            centernet.setConveyancegroup("Belt");
            centernet.setConveyancetype(null);
            centernet.setResourcesystem("Mcms");
            centernet.setGrouppackageamount(null);
            centernet.setGrouppackageunit(null);
            centernet.setPackageamount(null);
            centernet.setPackageunit(null);
            centernet.setTheoryamount(null);
            centernet.setGross(item.getWeight());
            centernet.setTare(BigDecimal.ZERO);
            centernet.setPonderationamount(item.getWeight());
            centernet.setMeasurebatch(null);
            centernet.setMeasurebatchcount(null);
            centernet.setMeasuretasknum(null);
            switch (item.getSenderType()) {
                case "部门":
                    centernet.setSendertype("Department");
                    break;
                case "库存地":
                    centernet.setSendertype("Storeroom");
                    break;
                case "企业（公司）":
                    centernet.setSendertype("Enterprise");
                    break;
                default:
                    centernet.setSendertype("");
                    ;
            }
            centernet.setSendername(item.getSenderName());
            centernet.setSender(item.getSenderCode());
            centernet.setSenderremark(null);
            switch (item.getRecieverType()) {
                case "部门":
                    centernet.setReceivertype("Department");
                    break;
                case "库存地":
                    centernet.setReceivertype("Storeroom");
                    break;
                case "企业（公司）":
                    centernet.setReceivertype("Enterprise");
                    break;
                default:
                    centernet.setReceivertype("");
                    ;
            }
            centernet.setReceivername(item.getRecieverName());
            centernet.setReceiver(item.getRecieverCode());
            centernet.setReceiverremark(null);
            centernet.setMaterialname(item.getMaterialName());
            centernet.setMaterial(item.getMaterialCode());
            centernet.setTaresystem("Mcms");
            centernet.setGrosssystem("Mcms");
            centernet.setNetsystem("Mcms");
            centernet.setTaredataresourcetype("WeightingInstrumentCollect");
            centernet.setGrossdataresourcetype("FixMeasureValue");
            centernet.setNetdataresourcetype("WeightingInstrumentCollect");
            centernet.setTaretime(item.getEndTime());
            centernet.setGrosstime(item.getEndTime());
            centernet.setNettime(item.getEndTime());
            centernet.setTarestationname(item.getStationName());
            centernet.setTarestation(item.getStationCode());
            centernet.setGrossstationname(item.getStationName());
            centernet.setGrossstation(item.getStationCode());
            centernet.setNetstationname(item.getStationName());
            centernet.setNetstation(item.getStationCode());
            centernet.setTareweightor(item.getAuditingManName());
            centernet.setGrossweightor(item.getAuditingManName());
            centernet.setNetweightor(item.getAuditingManName());
            centernet.setBatchidentity(null);
            centernet.setBusinessstate("NetAlready");
            centernet.setBilltype("BeltBill");
            centernet.setMeasuretype("MeasureMode6");
            centernet.setAmountunit("Z40");
            centernet.setDownloadedtag("N");
            centernet.setDeletedtag("N");
            centernet.setCreator("Mcms");
            centernet.setCreatedate(new Date());
            centernet.setChanger("Mcms");
            centernet.setChangedate(new Date());
            centernets.add(centernet);
        });
        int i = centernetMapper.batchInsert(centernets);
        Assert.isTrue(i == centernets.size(), "上传皮卡失败");
    }

    @Override
//    @TargetDataSource(dataSourceKey = DataSourceKey.DB_SLAVE2)
    public void cancelUpload(List<String> list) {
        List<Centernet> centernets = new ArrayList<>();
        for (String pikaDataId : list) {
            Centernet centernet = this.getById(pikaDataId);
            Assert.notNull(centernet, "数据不存在：" + pikaDataId);
            centernet.setDeletedtag("Y");
            centernets.add(centernet);
        }
        int i = centernetMapper.cancelUpload(centernets);
        Assert.isTrue(i == centernets.size(), "撤销上传皮卡失败");
//        centernetMapper.batchDelete(list);
    }

    @Override
//    @TargetDataSource(dataSourceKey = DataSourceKey.DB_SLAVE2)
    public Centernet myGetById(String identity) {
        return centernetMapper.myGetById(identity);
    }
}
