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

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.constant.SourceConstants;
import com.sifang.modules.sunshine.SystemSetting.dao.OrderMarkDao;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmOrderMarkSettings;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author lzp
 * @date 2019/10/11 14:28
 */
@Service
public class OrderMarkSettingService extends BaseService {

	@Autowired
	private OrderMarkDao markDao;

	public void getAllMarkData(LayuiPage page,String type) {
		List<Record> list = markDao.getAllMarkData(page,type);
		page.setData(list);
		page.setCount(markDao.getAllMarkotalCount(type));
	}

	public List<Record> getAllMarkType(String type){
		List<Record> list = Db.find("select id,name,type from crm_order_mark_settings a where a.status='0' and use_type=?",type);
		return list;
	}


	public Record getMarkDetail(String id){
		return Db.findFirst("select * from crm_order_mark_settings a where a.status='0' and a.id=?", id);
	}

	/**
	 * 添加标记
	 * @param map
	 * @return
	 */
	public String addMark(Map<String, String> map) {
		//校验该标记是否添加过
		long count = 0;
		long nameCount = 0;
		count = Db.queryLong("select count(1) from crm_order_mark_settings a where a.type=? and a.status='0' and use_type=?", map.get("type"),map.get("useType"));
		nameCount = Db.queryLong("select count(1) from crm_order_mark_settings a where a.name=? and a.status='0' and use_type=?", map.get("name"),map.get("useType"));
		if (count > 0) {
			return "202";//已添加过
		}
		if (nameCount > 0) {
			return "205";//工单标记名称已添加过
		}
		if(StringUtils.isBlank(map.get("name")) || StringUtils.isBlank(map.get("type"))){
			return "203";//必要参数为空
		}

		CrmOrderMarkSettings markSettings = new CrmOrderMarkSettings();
		markSettings.setCreateBy(UserUtils.getUser().getId());
		markSettings.setCreateTime(new Date());
		markSettings.setName(map.get("name"));
		if(StringUtils.isNotBlank(map.get("sort"))){
			markSettings.setSort(Integer.parseInt(map.get("sort")));
		}
		if(StringUtil.isBlank(map.get("useType"))){
			markSettings.setUseType(SourceConstants.ONE);
		}else{
			markSettings.setUseType(map.get("useType"));
		}
		markSettings.setStatus(SourceConstants.ZERO);
		markSettings.setRoleType(SourceConstants.ZERO);
		markSettings.setRoleTypeId(UserUtils.getUser().getId());
		//标记颜色（存具体颜色值红：#FF0000 绿：#008000 蓝：#0000FF 黄：#FFCC00 紫：#800080）
		markSettings.setType(map.get("type"));
		markDao.save(markSettings);
		return "200";
	}

	/**
	 * 修改标记
	 * @param map
	 * @return
	 */
	public String editMark(Map<String, String> map) {
		CrmOrderMarkSettings markSettings = markDao.get(map.get("id").toString());
		markSettings.setName(map.get("name").toString());
		if(StringUtils.isNotBlank((CharSequence) map.get("sort"))){
			markSettings.setSort(Integer.parseInt(map.get("sort").toString()));
		}
		markDao.save(markSettings);
		return "200";
	}

	public void delMark(String ids){
		Db.update("update crm_order_mark_settings a set a.status='1' where id in("+ StringUtil.joinInSql(ids.split(","))+") ");
	}

}
