package com.sifang.modules.sunshine.SystemSetting.service;


import com.google.common.collect.Lists;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.config.SfCacheService;
import com.sifang.modules.sunshine.SystemSetting.dao.TraderDao;
import com.sifang.modules.sunshine.SystemSetting.dao.TraderPartnerDao;
import com.sifang.modules.sunshine.SystemSetting.dao.TraderPcdDao;
import com.sifang.modules.sunshine.SystemSetting.dao.TraderProjectDao;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmTrader;
import com.sifang.modules.sunshine.SystemSetting.entity.TraderPartner;
import com.sifang.modules.sunshine.SystemSetting.entity.TraderPcd;
import com.sifang.modules.sunshine.SystemSetting.entity.TraderProject;
import com.sifang.modules.sunshine.SystemSetting.form.AddAreaVo;
import com.sifang.modules.sunshine.SystemSetting.form.AddProjectPowerVo;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.persistence.Parameter;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 代理商授权信息Service
 * @author lzp
 * @version 
 */
@Component
@Transactional(readOnly = true)
public class TraderProjectService extends BaseService {

	@Autowired
	private TraderProjectDao traderProjectDao;
	@Autowired
	SfCacheService cacheService;
	@Autowired
	TraderDao traderDao;

	@Autowired
	private TraderPcdDao traderPcdDao;
	@Autowired
	private TraderPartnerDao traderPartnerDao;

	@Transactional(rollbackFor = Exception.class, readOnly = false)
	public void saveTraderProject(AddProjectPowerVo powerVo) {
		// 保存项目
		List<TraderProject> lists = Lists.newArrayList();
		for (String proId : powerVo.getProjectIds()) {
			TraderProject pro = new TraderProject();
			pro.setProjectId(proId);
			pro.setTraderId(powerVo.getTraderId());
			lists.add(pro);
		}

		List<TraderPartner> partnerList = Lists.newArrayList();
		for (String partnerId : powerVo.getPartnerIds()) {
			TraderPartner partner = new TraderPartner();
			partner.setPartnerId(partnerId);
			partner.setTraderId(powerVo.getTraderId());
			partnerList.add(partner);
		}

		Date now = new Date();
		// 保存区域设置
		List<TraderPcd> pcdList = Lists.newArrayList();
		int i = 1;
		for (AddAreaVo vo : powerVo.getAreaList()) {
			TraderPcd pcd = new TraderPcd();
			pcd.setTraderId(powerVo.getTraderId());
			pcd.setProvince(vo.getProvince());
			if (StringUtil.isNotBlank(vo.getCity())) {
				pcd.setCity(vo.getCity());
			}
			if (StringUtil.isNotBlank(vo.getArea())) {
				pcd.setArea(vo.getArea());
			}
			pcd.setSort(i);
			pcd.setCreateTime(now);
			pcdList.add(pcd);
			i++;
		}
		//CrmTrader trader = traderDao.get(traderId);
		//traderDao.save(trader);
		if (lists.size() >= 0) {
			traderProjectDao.updateBySql("DELETE FROM `crm_trader_project`  WHERE `trader_id` =:p1", new Parameter(powerVo.getTraderId()));
			if (lists.size() > 0){
				traderProjectDao.save(lists);
			}
		}
		if (partnerList.size() >= 0) {
			traderPartnerDao.updateBySql("DELETE FROM `crm_trader_partner`  WHERE `trader_id` =:p1", new Parameter(powerVo.getTraderId()));
			if (partnerList.size() > 0){
				traderPartnerDao.save(partnerList);
			}
		}
		if (pcdList.size() > 0) {
			traderPcdDao.updateBySql("DELETE FROM crm_trader_pcd WHERE trader_id = :p1", new Parameter(powerVo.getTraderId()));
			traderPcdDao.save(pcdList);
		}
		cacheService.del(powerVo.getTraderId() + "projectList");
	}
	
	@Transactional(readOnly = false)
	public void delete(String id) {
		traderProjectDao.deleteById(id);
	}

	public List<Record> getTraderProject(String traderId) {
		List<Record> list = traderProjectDao.findTraderProject(traderId);
		for (Record r : list) {
			if (Objects.equals(r.getStr("status"), "0")) {
				r.set("status", "启用");
			} else if (Objects.equals(r.getStr("status"), "2")) {
				r.set("status", "禁用");
			} else {
				r.set("status", "删除");
			}
			if (Objects.equals(r.getStr("filingType"), "1")) {
				r.set("filingType", "农户备案");
			} else {
				r.set("filingType", "项目公司备案");
			}
		}
		return list;
	}
	
}
