package com.xisc.pm.infra.repository.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.hzero.mybatis.base.impl.BaseRepositoryImpl;
import org.hzero.mybatis.domian.Condition;
import org.hzero.mybatis.util.Sqls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.xisc.pm.domain.entity.PmSlabHeatDest;
import com.xisc.pm.domain.entity.PmSlabHeatDestTra;
import com.xisc.pm.domain.repository.PmSlabHeatDestRepository;
import com.xisc.pm.domain.repository.PmSlabHeatDestTraRepository;

import io.choerodon.core.oauth.DetailsHelper;

/**
 * 
 * @author jun.zeng
 *
 */
@Component
public class PmSlabHeatDestRepositoryImpl extends BaseRepositoryImpl<PmSlabHeatDest>
        implements PmSlabHeatDestRepository {

    @Autowired
    private PmSlabHeatDestTraRepository pmSlabHeatDestTraRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSlabHeatDestBySlab(String slabNo, Map<String, String> heatTreatmentModeMap, Long tenantId) {
        List<PmSlabHeatDest> historyHeatDests = this.selectByCondition(Condition.builder(PmSlabHeatDest.class)
                .andWhere(Sqls.custom().andEqualTo(PmSlabHeatDest.FIELD_TENANT_ID, tenantId)
                        .andEqualTo(PmSlabHeatDest.FIELD_SLAB_NO, slabNo))
                .build());
        if (historyHeatDests != null) {
            // 迁移到履历表
            List<PmSlabHeatDestTra> heatDestTras = new ArrayList<>();
            String operationUser = DetailsHelper.getUserDetails() == null ? ""
                    : DetailsHelper.getUserDetails().getUserId().toString();
            for (PmSlabHeatDest pmSlabHeatDest : historyHeatDests) {
                PmSlabHeatDestTra pmSlabHeatDestTra = new PmSlabHeatDestTra();
                pmSlabHeatDestTra.setPshdId(pmSlabHeatDest.getPshdId());
                pmSlabHeatDestTra.setSlabNo(pmSlabHeatDest.getSlabNo());
                pmSlabHeatDestTra.setHeatTreatmentMode(pmSlabHeatDest.getHeatTreatmentMode());
                pmSlabHeatDestTra.setDefaultDest(pmSlabHeatDest.getDefaultDest());
                pmSlabHeatDestTra.setOperationType("I");
                pmSlabHeatDestTra.setOperationTime(new Date());
                pmSlabHeatDestTra.setOperationUser(operationUser);
                heatDestTras.add(pmSlabHeatDestTra);
            }
            pmSlabHeatDestTraRepository.batchInsertSelective(heatDestTras);
            this.batchDeleteByPrimaryKey(historyHeatDests);
        }
        List<PmSlabHeatDest> heatDests = new ArrayList<>();
        for (Entry<String, String> heatTreatmentMode : heatTreatmentModeMap.entrySet()) {
            PmSlabHeatDest pmSlabHeatDest = new PmSlabHeatDest();
            pmSlabHeatDest.setHeatTreatmentMode(heatTreatmentMode.getKey());
            pmSlabHeatDest.setSlabNo(slabNo);
            pmSlabHeatDest.setDefaultDest(heatTreatmentMode.getValue());
            pmSlabHeatDest.setTenantId(tenantId);
            heatDests.add(pmSlabHeatDest);
        }
        this.batchInsertSelective(heatDests);
    }

}
