package com.steerinfo.ems.trmelectricitylocation.service.impl;

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.model.IUpdatePO;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.ems.trmelectricitylocation.model.TRmElectricityLocation;
import com.steerinfo.ems.trmelectricitylocation.mapper.TRmElectricityLocationMapper;
import com.steerinfo.ems.trmelectricitylocation.service.ITRmElectricityLocationService;
import com.steerinfo.ems.trmtransfereactvalue.model.TRmTransfereActValue;
import com.steerinfo.ems.trmtransfereactvalue.service.ITRmTransfereActValueService;
import com.steerinfo.ems.trmworkproc.mapper.TRmWorkprocMapper;
import com.steerinfo.ems.trmworkproc.model.TRmWorkproc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.math.BigDecimal;

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

    @Autowired
    private TRmElectricityLocationMapper tRmElectricityLocationMapper;
    
    @Autowired
    private TRmWorkprocMapper tRmWorkprocMapper;
    
    @Autowired
    private ITRmTransfereActValueService tRmTransfereActValueService;

    @Override
    protected IBaseMapper<TRmElectricityLocation, String> getMapper() {
        return tRmElectricityLocationMapper;
    }

    public TRmElectricityLocation updateByUkey(TRmElectricityLocation model){
        if (model instanceof IUpdatePO) {
            this.setUpdate((IUpdatePO)model);
        }
        // 原数据
        TRmElectricityLocation oel = this.getById(model.getId());
        String newWorkshop = model.getWorkshop();
        String oldWorkshop = oel.getWorkshop();
        String newWorkprocid = model.getWorkprocid();
        String oldWorkprocid = oel.getWorkprocid();
        String newLocation = model.getLocation();
        String oldLocation = oel.getLocation();
        
        tRmElectricityLocationMapper.updateByPrimaryKeySelective(model);
        if(!newWorkshop.equalsIgnoreCase(oldWorkshop) || !newWorkprocid.equalsIgnoreCase(oldWorkprocid) || !newLocation.equalsIgnoreCase(oldLocation)) {
        	// System.out.println("同步更新转供电实绩表数据");
	        List<TRmTransfereActValue> list = tRmTransfereActValueService.queryByProperty("locationid", model.getId());
	        for(TRmTransfereActValue tt : list) {
	        	tt.setWorkshop(model.getWorkshop());
	        	tt.setWorkprocid(model.getWorkprocid());
	        	tt.setLocation(model.getLocation());
	        	tRmTransfereActValueService.modify(tt);
	        }
        }
        return tRmElectricityLocationMapper.selectByPrimaryKey(model.getId());
    }

	@Override
	public List<TRmWorkproc> queryWorkprocs(Map<String, Object> parmas) {
		return tRmWorkprocMapper.queryWorkprocs(parmas);
	}
}
