package com.prolog.rdc.tms.service.impl;

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

import com.prolog.rdc.tms.model.dto.CityZoneDTO;
import com.prolog.rdc.tms.model.dto.DistrictZoneDTO;
import com.prolog.rdc.tms.model.dto.LmsTmsZoneMinDTO;
import com.prolog.rdc.tms.model.dto.ProvinceZoneDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.prolog.common.util.ToolKits;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Restriction;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.framework.dao.util.PageUtils;
import com.prolog.product.core.framework.entity.UserEntity;
import com.prolog.product.core.framework.util.PlgFxUsrUtils;
import com.prolog.rdc.tms.constant.BussinessConstant;
import com.prolog.rdc.tms.dao.mapper.LmsTmsZoneMapper;
import com.prolog.rdc.tms.model.dto.LmsTmsZoneDTO;
import com.prolog.rdc.tms.model.entity.LmsTmsZoneModel;
import com.prolog.rdc.tms.model.vo.LmsTmsZoneQueryVO;
import com.prolog.rdc.tms.model.vo.LmsTmsZoneVO;
import com.prolog.rdc.tms.service.LmsTmsZoneService;

/**
 * 
 *
 * 行政区域 IMPL
 *
 * @className :LmsTmsZoneServiceImpl
 * @author :fanzw
 * @creationTime :2020/07/03
 * @description :
 * @modifier :fanzw
 * @modifyTime :2020/07/03
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Service
@Transactional(readOnly = true)
public class LmsTmsZoneServiceImpl implements LmsTmsZoneService {

	@Autowired
	private LmsTmsZoneMapper zoneMapper;

	/**
	 * 根据上级编号(fatherid)获取区域信息
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public List<LmsTmsZoneDTO> getZoneByFatherid(LmsTmsZoneVO lmstmszonevo) {
		// 根据父节点查询行政区域
		Criteria criteria = Criteria.forClass(LmsTmsZoneModel.class);
		Restriction r1 = null;
		if (ToolKits.isEmpty(lmstmszonevo)) {
			r1 = Restrictions.isnull("fatherid");
		} else {
			if (ToolKits.isEmpty(lmstmszonevo.getFatherid())) {
				r1 = Restrictions.isnull("fatherid");
			} else {
				r1 = Restrictions.in("fatherid", lmstmszonevo.getFatherid().split(","));
			}
		}
		criteria.setRestriction(Restrictions.and(r1));
		// 执行查询
		List<LmsTmsZoneModel> lmstmszonemodels = zoneMapper.findByCriteria(criteria);
		// 响应集合
		List<LmsTmsZoneDTO> lmstmszonedtos = new ArrayList<LmsTmsZoneDTO>();
		for (LmsTmsZoneModel lmstmszonemodel : lmstmszonemodels) {
			// 响应对象
			LmsTmsZoneDTO lmstmszonedto = new LmsTmsZoneDTO();
			BeanUtils.copyProperties(lmstmszonemodel, lmstmszonedto);
			lmstmszonedtos.add(lmstmszonedto);
		}

		return lmstmszonedtos;

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public LmsTmsZoneModel getZoneById(String zoneid) {
		return zoneMapper.findById(zoneid, LmsTmsZoneModel.class);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public List<LmsTmsZoneModel> getZoneByIds(List<String> zoneIds) {
		return zoneMapper.findByIds(LmsTmsZoneModel.class,zoneIds.stream().toArray(), null);
	}

	// 行政区域删除及子节点
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteZone(List<LmsTmsZoneModel> list) throws Exception {
		ToolKits.isParamNull(list, "参数不能为空!");

		for (LmsTmsZoneModel lmsTmsZone : list) {
			ToolKits.isParamNull(lmsTmsZone.getZoneid(), "区域ID不为空");

			// 查询此节点下面所有的子节点
			List<LmsTmsZoneModel> list_new = this.getZoneListByFatherid(lmsTmsZone.getZoneid());
			// 若此节点下面没有子节点
			if (list_new.size() == 0) {
				LmsTmsZoneModel lmsTmsZone_new = zoneMapper.findById(lmsTmsZone.getZoneid(), LmsTmsZoneModel.class);
				if (ToolKits.isEmpty(lmsTmsZone_new)) {
					throw new RuntimeException("您输入的当前id不正确,请重新输入!");
				}
				// 删除此节点
				this.deleteZoneById(lmsTmsZone.getZoneid());
			} else {
				this.deleteZoneById(lmsTmsZone.getZoneid());
				for (LmsTmsZoneModel lmsTmsZone_new_flag : list_new) {
					List<LmsTmsZoneModel> list_new_flag = this.getZoneListByFatherid(lmsTmsZone_new_flag.getZoneid());
					if (list_new_flag.size() > 0) {
						deleteZone(list_new);
					} else {
						this.deleteZoneById(lmsTmsZone_new_flag.getZoneid());
					}
				}
			}
		}
	}

	/**
	 * 行政区域新增
	 */
	@Override
	public void insertZoneData(LmsTmsZoneVO lmstmszonevo) throws Exception {
		ToolKits.isParamNull(lmstmszonevo, "行政区域新增请求报文不为空");
		ToolKits.isParamNull(lmstmszonevo.getZoneid(), "区域ID不为空");
		ToolKits.isParamNull(lmstmszonevo.getZname(), "区域名称不为空");
		ToolKits.isParamNull(lmstmszonevo.getKeyvalue(), "简称不为空");
		ToolKits.isParamNull(lmstmszonevo.getZlevel(), "层级不为空");

		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		if (BussinessConstant.ZONE_ZLEVEL.compareTo(lmstmszonevo.getZlevel()) != 0) {
			ToolKits.isParamNull(lmstmszonevo.getFatherid(), "父级ID不为空");
			ToolKits.isParamNull(lmstmszonevo.getFullname(), "父级名称不为空");
		}

		// 判断当前区域ID是否被占用
		LmsTmsZoneModel lmstmszone = zoneMapper.findById(lmstmszonevo.getZoneid(), LmsTmsZoneModel.class);
		if (ToolKits.isNotEmpty(lmstmszone)) {
			throw new RuntimeException("区域ID[" + lmstmszonevo.getZoneid() + "]已存在");
		}

		LmsTmsZoneModel lmstmszonemodel = new LmsTmsZoneModel();
		BeanUtils.copyProperties(lmstmszonevo, lmstmszonemodel);
		lmstmszonemodel.setCreateperson(user.getNickname());
		lmstmszonemodel.setCreatetime(new Date());
		// 保存
		zoneMapper.save(lmstmszonemodel);
	}

	/**
	 * 行政区域修改
	 */
	@Override
	public void updateZoneByIdData(LmsTmsZoneVO lmstmszonevo) throws Exception {
		ToolKits.isParamNull(lmstmszonevo, "行政区域修改请求报文不为空");
		ToolKits.isParamNull(lmstmszonevo.getZoneid(), "区域ID不为空");
		ToolKits.isParamNull(lmstmszonevo.getZname(), "区域名称不为空");
		ToolKits.isParamNull(lmstmszonevo.getKeyvalue(), "简称不为空");
		ToolKits.isParamNull(lmstmszonevo.getZlevel(), "层级不为空");

		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		if (BussinessConstant.ZONE_ZLEVEL.compareTo(lmstmszonevo.getZlevel()) != 0) {
			ToolKits.isParamNull(lmstmszonevo.getFatherid(), "父级ID不为空");
			ToolKits.isParamNull(lmstmszonevo.getFullname(), "父级名称不为空");
		}

		// 根据区域ID查询行政区域
		LmsTmsZoneModel lmstmszonemodel = zoneMapper.findById(lmstmszonevo.getZoneid(), LmsTmsZoneModel.class);
		if (ToolKits.isEmpty(lmstmszonemodel)) {
			throw new RuntimeException("区域ID[" + lmstmszonevo.getZoneid() + "]不存在");
		}

		BeanUtils.copyProperties(lmstmszonevo, lmstmszonemodel);
		lmstmszonemodel.setUpdateperson(user.getNickname());
		lmstmszonemodel.setUpdatetime(new Date());
		zoneMapper.update(lmstmszonemodel);
	}

	private List<LmsTmsZoneModel> getZoneListByFatherid(String zoneid) {
		return zoneMapper.getZoneListByFatherid(zoneid);

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteZoneById(String zoneid) {
		if (zoneid == null || "".equals(zoneid)) {
			throw new RuntimeException("区域id不能为空");
		}

		zoneMapper.deleteById(zoneid, LmsTmsZoneModel.class);
	}

	@Override
	public List<LmsTmsZoneDTO> getZone() {
		List<LmsTmsZoneDTO> list = zoneMapper.getZone(null);
		return list;
	}

	// 区划分页查询
	@Override
	public Page<LmsTmsZoneDTO> getZoneByPage(int pageNum, int pageSize) {
		PageUtils.startPage(pageNum, pageSize);
		List<LmsTmsZoneDTO> list = zoneMapper.getZone(null);
		return PageUtils.getPage(list);
	}

	@Override
	public List<LmsTmsZoneDTO> getFatheridIsNull() {
		// 行政区域查询无父节点数据
		Criteria criteria = Criteria.forClass(LmsTmsZoneModel.class);
		Restriction r1 = Restrictions.isnull("fatherid");
		criteria.setRestriction(Restrictions.and(r1));
		// 执行查询
		List<LmsTmsZoneModel> lmstmszonemodels = zoneMapper.findByCriteria(criteria);
		// 响应集合
		List<LmsTmsZoneDTO> lmstmszonedtos = new ArrayList<LmsTmsZoneDTO>();
		for (LmsTmsZoneModel lmstmszonemodel : lmstmszonemodels) {
			// 响应对象
			LmsTmsZoneDTO lmstmszonedto = new LmsTmsZoneDTO();
			BeanUtils.copyProperties(lmstmszonemodel, lmstmszonedto);
			lmstmszonedtos.add(lmstmszonedto);
		}

		return lmstmszonedtos;
	}

	@Override
	public List<Map<String, Object>> getDropDownList(Map<String, Object> param) {
		String zoneid = param.get("fatherid") == null ? "01" : (String) param.get("fatherid");
		return zoneMapper.getDropDownList(zoneid);
	}

	/**
	 * 根据名称查询行政区域编码
	 */
	@Override
	public List<LmsTmsZoneDTO> getLmsTmsZoneByNameData(LmsTmsZoneQueryVO lmstmszonequeryvo) throws Exception {
		ToolKits.isParamNull(lmstmszonequeryvo, "根据名称查询行政区域编码请求报文不为空");
		ToolKits.isParamNull(lmstmszonequeryvo.getZname(), "行政区域名称不为空");
		ToolKits.isParamNull(lmstmszonequeryvo.getZlevel(), "行政区域层级不为空");

		// 行政区域查询
		Criteria criteria = Criteria.forClass(LmsTmsZoneModel.class);
		Restriction r1 = Restrictions.eq("zname", lmstmszonequeryvo.getZname());
		Restriction r2 = Restrictions.eq("zlevel", lmstmszonequeryvo.getZlevel());
		Restriction r3 = ToolKits.isEmpty(lmstmszonequeryvo.getFatherid()) ? null : Restrictions.eq("fatherid", lmstmszonequeryvo.getFatherid());
		criteria.setRestriction(Restrictions.and(r1, r2, r3));

		List<LmsTmsZoneDTO> lmstmszonedtos = new ArrayList<LmsTmsZoneDTO>();
		// 执行查询
		List<LmsTmsZoneModel> lmstmszonemodels = zoneMapper.findByCriteria(criteria);
		for (LmsTmsZoneModel lmstmszonemodel : lmstmszonemodels) {
			LmsTmsZoneDTO lmstmszonedto = new LmsTmsZoneDTO();
			BeanUtils.copyProperties(lmstmszonemodel, lmstmszonedto);
			lmstmszonedtos.add(lmstmszonedto);
		}

		return lmstmszonedtos;
	}

	@Override
	public Page<LmsTmsZoneModel> getZoneByConditions(LmsTmsZoneVO lmstmszonevo) {
		ToolKits.isParamNull(lmstmszonevo, "根据名称查询行政区域编码请求报文不为空");
		ToolKits.isParamNull(lmstmszonevo.getPageNum(), "页码不能为空");
		ToolKits.isParamNull(lmstmszonevo.getPageSize(), "页数不能为空");
		PageUtils.startPage(lmstmszonevo.getPageNum(), lmstmszonevo.getPageSize());
		// 行政区域查询
		Criteria criteria = Criteria.forClass(LmsTmsZoneModel.class);
		Restriction or1 = Restrictions.like("zoneid", "%" + lmstmszonevo.getZname() + "%");
		Restriction or2 = Restrictions.like("zname", "%" + lmstmszonevo.getZname() + "%");
		Restriction r1 = ToolKits.isEmpty(lmstmszonevo.getZname()) ? null : Restrictions.or(or1, or2);
		criteria.setRestriction(r1);
		// 执行查询
		List<LmsTmsZoneModel> lmstmszonemodels = zoneMapper.findByCriteria(criteria);
		return PageUtils.getPage(lmstmszonemodels);
	}

	/**
	 * 获取所有行政区
	 *
	 * @return
	 */
	@Override
	public List<LmsTmsZoneDTO> getAllZone() {
		Criteria criteria = Criteria.forClass(LmsTmsZoneModel.class);
		List<LmsTmsZoneModel> tmsZoneModels = zoneMapper.findByCriteria(criteria);

		List<LmsTmsZoneDTO> lmsTmsZoneDTOS = Lists.newArrayList();
		for (LmsTmsZoneModel tmsZoneModel : tmsZoneModels) {
			// 响应对象
			LmsTmsZoneDTO lmsTmsZoneDTO = new LmsTmsZoneDTO();
			BeanUtils.copyProperties(tmsZoneModel, lmsTmsZoneDTO);
			lmsTmsZoneDTOS.add(lmsTmsZoneDTO);
		}
		return lmsTmsZoneDTOS;
	}

	/**
	 * 获取所有行政区
	 *
	 * @return
	 */
	public List<ProvinceZoneDTO> getAllProvinceCityDistrict() {

		List<ProvinceZoneDTO> provinceZoneDTOS = Lists.newArrayList();
		List<LmsTmsZoneDTO> lmsTmsZoneDTOS = zoneMapper.getlProvinceZone(2,null);
		for (LmsTmsZoneDTO zoneDTO1: lmsTmsZoneDTOS) {
			ProvinceZoneDTO provinceZoneDTO = new ProvinceZoneDTO();
			provinceZoneDTO.setZoneid(zoneDTO1.getZoneid());
			provinceZoneDTO.setZname(zoneDTO1.getZname());

			// 查询省下面的市
			List<LmsTmsZoneDTO> cityZoneDTOS = zoneMapper.getlProvinceZone(3,provinceZoneDTO.getZoneid());
			List<CityZoneDTO> cityZoneDTOList = Lists.newArrayList();
			for (LmsTmsZoneDTO zoneDTO2: cityZoneDTOS) {
				CityZoneDTO cityZoneDTO = new CityZoneDTO();
				cityZoneDTO.setZoneid(zoneDTO2.getZoneid());
				cityZoneDTO.setZname(zoneDTO2.getZname());

				// 查询市下面的区
				List<LmsTmsZoneDTO> DistrictZoneDTOS = zoneMapper.getlProvinceZone(4,cityZoneDTO.getZoneid());
				List<DistrictZoneDTO> districtZoneDTOList = Lists.newArrayList();
				for (LmsTmsZoneDTO zoneDTO3: DistrictZoneDTOS) {
					DistrictZoneDTO districtZoneDTO = new DistrictZoneDTO();
					districtZoneDTO.setZoneid(zoneDTO3.getZoneid());
					districtZoneDTO.setZname(zoneDTO3.getZname());
					districtZoneDTOList.add(districtZoneDTO);
				}
				cityZoneDTO.setDistrictZoneDTOS(districtZoneDTOList);
				cityZoneDTOList.add(cityZoneDTO);
			}
			provinceZoneDTO.setCityZoneDTOList(cityZoneDTOList);
			provinceZoneDTOS.add(provinceZoneDTO);
		}

		return provinceZoneDTOS;
	}

	@Override
	public List<LmsTmsZoneMinDTO> getAllZoneMin() {
		return zoneMapper.getAllZoneMin();
	}

}
