/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.service.sysmgr.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.crunii.micro.common.constants.CommonConstants;
import com.crunii.micro.common.dto.PageReq;
import com.crunii.micro.common.dto.PageRsp;
import com.crunii.micro.common.exception.BusinessException;
import com.crunii.micro.common.utils.JsonUtil;
import com.crunii.micro.core.cache.TsmPropertyCache;
import com.crunii.micro.domain.entity.main.TsmProperty;
import com.crunii.micro.domain.entity.main.TsmPropertyValue;
import com.crunii.micro.domain.repo.main.TsmPropertyRepo;
import com.crunii.micro.domain.repo.main.TsmPropertyValueRepo;
import com.crunii.micro.service.sysmgr.dto.sysmgr.*;
import com.crunii.micro.service.sysmgr.jdbc.PropertyJdbcDao;
import com.crunii.micro.service.sysmgr.service.PropertyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author 田平 create 2019年2月20日下午4:03:35
 */
@Service
public class PropertyServiceImpl implements PropertyService {
	@Autowired
	private TsmPropertyRepo tsmPropertyRepo;
	@Autowired
	private TsmPropertyValueRepo tsmPropertyValueRepo;
	@Autowired
	private PropertyJdbcDao propertyJdbcDao;
	@Autowired
	private TsmPropertyCache cache;

	public List<TsmPropertyValue> getValuesByCode(String propCode) {
//		TsmProperty prop = tsmPropertyRepo.getUniqueByProp("propCode", propCode);
		return tsmPropertyValueRepo.findByProp("propCode", propCode);
	}

	public PageRsp<TsmPropertyValue> pageValue(PageReq<ValueReq> req) {
		return propertyJdbcDao.pageValue(req);
	}

	public PageRsp<TsmProperty> pageProperty(PageReq<PropertyReq> req) {
		return propertyJdbcDao.pageProperty(req);
	}

	public void createProperty(PropertyC propertyC) {
		TsmProperty property = new TsmProperty();
		BeanUtils.copyProperties(propertyC, property);
		property.setCreateTime(new Date());
		property.setState(1);
		property.setWorkerManageState(1);
		tsmPropertyRepo.save(property);
	}

	public void updateProperty(PropertyU propertyU) {
		TsmProperty property = tsmPropertyRepo.getOne(propertyU.getPropId());
		// 如修改静态值code 需更新
		if(!propertyU.getPropCode().equals(property.getPropCode())){
			List<TsmPropertyValue> byPropIdAndState = tsmPropertyValueRepo.findByPropIdAndState(property.getPropId(), 1);
			// 如为空，则证明property下无数据，则return
			if(CollectionUtil.isEmpty(byPropIdAndState)){
				return;
			}
			for (TsmPropertyValue tsmPropertyValue : byPropIdAndState) {
				tsmPropertyValue.setPropCode(propertyU.getPropCode());
			}
			tsmPropertyValueRepo.saveAllAndFlush(byPropIdAndState);
			cache.reload();
		}
		BeanUtils.copyProperties(propertyU, property);
		property.setWorkerManageState(propertyU.getWorkerManageState());
		tsmPropertyRepo.save(property);
	}

	public void createValue(ValueC valueC) {
		TsmPropertyValue tsmPropertyValue = tsmPropertyValueRepo.findTsmPropertyValueByValueCodeAndPropCode(valueC.getValueCode(), valueC.getPropCode());
		if(Objects.nonNull(tsmPropertyValue)){
			throw new BusinessException("名称已存在");
		}
		List<TsmProperty> prop = tsmPropertyRepo.findByProp("propCode", valueC.getPropCode());
		TsmPropertyValue value = new TsmPropertyValue();
		BeanUtils.copyProperties(valueC, value);
		value.setCreateTime(new Date());
		value.setState(1);
		value.setPropId(prop.get(0).getPropId());
		tsmPropertyValueRepo.save(value);
		cache.reload();
	}

	public void updateValue(ValueU valueU) {
		TsmPropertyValue value = tsmPropertyValueRepo.getOne(valueU.getValueId());
		// 如果修改静态值code，则判断当前propcode是否存在值code
		if(!value.getValueCode().equals(valueU.getValueCode())){
			TsmPropertyValue tsmPropertyValue = tsmPropertyValueRepo.findTsmPropertyValueByValueCodeAndPropCode(valueU.getValueCode(), value.getPropCode());
			if(Objects.nonNull(tsmPropertyValue)){
				throw new BusinessException("名称已存在");
			}
		}
		BeanUtils.copyProperties(valueU, value);
		tsmPropertyValueRepo.save(value);
		cache.reload();
	}

	@Override
	public void deleteValue(String propCode, Integer valueId) {
		TsmPropertyValue value = tsmPropertyValueRepo.getById(valueId);
		if (value != null && StrUtil.equals(value.getPropCode(), propCode)) {
			value.setDelFlag(1);
			tsmPropertyValueRepo.save(value);
			cache.reload();
		}
	}

	@Override
	public void createValueList(ValueC valueC1) {
		List<TsmPropertyValue> list = new ArrayList<>();
		AtomicInteger atomicInteger = new AtomicInteger(2);
		String a = "{\n" +
				"  \"OC10100\": \"中国共产党机关负责人\",\n" +
				"  \"OC10200\": \"国家机关负责人\",\n" +
				"  \"OC10300\": \"民主党派和工商联负责人\",\n" +
				"  \"OC10400\": \"人民团体和群众团体、社会组织及其他成员组织负责人\",\n" +
				"  \"OC10500\": \"基层群众自治组织负责人\",\n" +
				"  \"OC10600\": \"企事业单位负责人\",\n" +
				"  \"OC20000\": \"专业技术人员\",\n" +
				"  \"OC20100\": \"科学研究人员\",\n" +
				"  \"OC20200\": \"工程技术人员\",\n" +
				"  \"OC20300\": \"农业技术人员\",\n" +
				"  \"OC20400\": \"飞机和船舶技术人员\",\n" +
				"  \"OC20500\": \"卫生专业技术人员\",\n" +
				"  \"OC20600\": \"经济和金融专业人员\",\n" +
				"  \"OC20700\": \"法律、社会和宗教专业人员\",\n" +
				"  \"OC20800\": \"教学人员\",\n" +
				"  \"OC20900\": \"文学艺术、体育专业人员\",\n" +
				"  \"OC21000\": \"新闻出版、文化专业人员\",\n" +
				"  \"OC29900\": \"其他专业技术人员\",\n" +
				"  \"OC30000\": \"办事人员和有关人员\",\n" +
				"  \"OC30100\": \"办事人员\",\n" +
				"  \"OC30200\": \"安全和消防人员\",\n" +
				"  \"OC39900\": \"其他办事人员和有关人员\",\n" +
				"  \"OC40000\": \"社会生产服务和生活服务人员\",\n" +
				"  \"OC40100\": \"批发与零售服务人员\",\n" +
				"  \"OC40200\": \"交通运输、仓储和邮政业服务人员\",\n" +
				"  \"OC40300\": \"住宿和餐饮服务人员\",\n" +
				"  \"OC40400\": \"信息传输、软件和信息技术服务人员\",\n" +
				"  \"OC40500\": \"金融服务人员\",\n" +
				"  \"OC40600\": \"房地产服务人员\",\n" +
				"  \"OC40700\": \"租赁和商务服务人员\",\n" +
				"  \"OC40800\": \"技术辅助服务人员\",\n" +
				"  \"OC40900\": \"水利、环境和公共设施管理服务人员\",\n" +
				"  \"OC41000\": \"居民服务人员\",\n" +
				"  \"OC41100\": \"电力、燃气及水供应服务人员\",\n" +
				"  \"OC41200\": \"修理及制作服务人员\",\n" +
				"  \"OC41300\": \"文化、体育和娱乐服务人员\",\n" +
				"  \"OC41400\": \"健康服务人员\",\n" +
				"  \"OC49900\": \"其他社会生产和生活服务人员\",\n" +
				"  \"OC50000\": \"农、林、牧、渔业生产及辅助人员\",\n" +
				"  \"OC50100\": \"农业生产人员\",\n" +
				"  \"OC50200\": \"林业生产人员\",\n" +
				"  \"OC50300\": \"畜牧业生产人员\",\n" +
				"  \"OC50400\": \"渔业生产人员\",\n" +
				"  \"OC50500\": \"农林牧渔生产辅助人员\",\n" +
				"  \"OC59900\": \"其他农、林、牧、渔业生产及辅助人员\",\n" +
				"  \"OC60000\": \"生产制造及有关人员\",\n" +
				"  \"OC60100\": \"农副产品加工人员\",\n" +
				"  \"OC60200\": \"食品、饮料生产加工人员\",\n" +
				"  \"OC60300\": \"烟草及其制品加工人员\",\n" +
				"  \"OC60400\": \"纺织、针织、印染人员\",\n" +
				"  \"OC60500\": \"纺织品、服装和皮革、毛皮制品加工制作人员\",\n" +
				"  \"OC60600\": \"木材加工、家具与木制品制作人员\",\n" +
				"  \"OC60700\": \"纸及纸制品生产加工人员\",\n" +
				"  \"OC60800\": \"印刷和记录媒介复制人员\",\n" +
				"  \"OC60900\": \"文教、工美、体育和娱乐用品制作人员\",\n" +
				"  \"OC61000\": \"石油加工和炼焦、煤化工生产人员\",\n" +
				"  \"OC61100\": \"化学原料和化学制品制造人员\",\n" +
				"  \"OC61200\": \"医药制造人员\",\n" +
				"  \"OC61300\": \"化学纤维制造人员\",\n" +
				"  \"OC61400\": \"橡胶和塑料制品制造人员\",\n" +
				"  \"OC61500\": \"非金属矿物制品制造人员\",\n" +
				"  \"OC61600\": \"采矿人员\",\n" +
				"  \"OC61700\": \"金属冶炼和压延加工人员\",\n" +
				"  \"OC61800\": \"机械制造基础加工人员\",\n" +
				"  \"OC61900\": \"金属制品制造人员\",\n" +
				"  \"OC62000\": \"通用设备制造人员\",\n" +
				"  \"OC62100\": \"专用设备制造人员\",\n" +
				"  \"OC62200\": \"汽车制造人员\",\n" +
				"  \"OC62300\": \"铁路、船舶、航空设备制造人员\",\n" +
				"  \"OC62400\": \"电气机械和器材制造人员\",\n" +
				"  \"OC62500\": \"计算机、通信和其他电子设备制造人员\",\n" +
				"  \"OC62600\": \"仪器仪表制造人员\",\n" +
				"  \"OC62700\": \"废弃资源综合利用人员\",\n" +
				"  \"OC62800\": \"电力、热力、气体、水生产和输配人员\",\n" +
				"  \"OC62900\": \"建筑施工人员\",\n" +
				"  \"OC63000\": \"运输设备和通用工程机械操作人员及有关人员\",\n" +
				"  \"OC63100\": \"生产辅助人员\",\n" +
				"  \"OC69900\": \"其他生产制造及有关人员\",\n" +
				"  \"OC70000\": \"军人\",\n" +
				"  \"OC80000\": \"不便分类的其他从业人员\"\n" +
				"}";
		Map<String, Object> map = JsonUtil.jsonToMap(a);
		Set<String> set = map.keySet();
		for (String s : set) {
			String o = map.get(s).toString();
			TsmPropertyValue tsmPropertyValue = new TsmPropertyValue();
			tsmPropertyValue.setPropId(valueC1.getPropId());
			tsmPropertyValue.setPropCode(valueC1.getPropCode());
			tsmPropertyValue.setValueCode(s);
			tsmPropertyValue.setValueName(o);
			tsmPropertyValue.setSortId(atomicInteger.getAndIncrement());
			tsmPropertyValue.setState(1);
			tsmPropertyValue.setCreateTime(new Date());
			list.add(tsmPropertyValue);
		}
		tsmPropertyValueRepo.saveAllAndFlush(list);
		cache.reload();
	}
}
