package cn.superdesk.uniorder.service.marketing.department.impl;

import cn.superdesk.uniorder.dao.entity.marketing.department.UnifiedAreaCity;
import cn.superdesk.uniorder.dao.mapper.marketing.department.*;
import cn.superdesk.uniorder.dto.vo._function.select.SelectOptionAreaItem;
import cn.superdesk.uniorder.dto.vo.marketing.department.UnifiedAreaCityWithVirt;
import cn.superdesk.uniorder.dto.vo.marketing.department.UnifiedAreaTJ;
import cn.superdesk.uniorder.dto.vo.marketing.department.UnifiedAreaWeb;
import cn.superdesk.uniorder.service.marketing.department.UnifiedAreaCityService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2020/11/24 下午5:38
 * @UpdateUser: linjinyu
 * @UpdateDate: 2020/11/24 下午5:38
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Service
public class UnifiedAreaCityServiceImpl implements UnifiedAreaCityService {

	@Autowired
	private UnifiedAreaCityMapper         unifiedAreaCityMapper;
	@Autowired
	private UnifiedAreaCityMapperEx       unifiedAreaCityMapperEx;
	@Autowired
	private UnifiedAreaCityVirtMapper     unifiedAreaCityVirtMapper;
	@Autowired
	private UnifiedAreaCityVirtMapperEx   unifiedAreaCityVirtMapperEx;
	@Autowired
	private UnifiedAreaCityTargetMapper   unifiedAreaCityTargetMapper;
	@Autowired
	private UnifiedAreaCityTargetMapperEx unifiedAreaCityTargetMapperEx;


	@Override
	public PageInfo<UnifiedAreaCity> findByPaging(Map<String, Object> params, Integer pageNo, Integer pageSize) {

		return null;
	}

	@Override
	public void deleteObj(Long id) {

		unifiedAreaCityMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(UnifiedAreaCity t) {

		return unifiedAreaCityMapper.updateByPrimaryKeySelective(t);
	}

	@Override
	public UnifiedAreaCity findById(Long id) {

		return unifiedAreaCityMapper.selectByPrimaryKey(id);
	}

	@Override
	public int insertSelective(UnifiedAreaCity t) {

		return unifiedAreaCityMapper.insertSelective(t);
	}

	@Override
	public List<UnifiedAreaCity> findAll(Map<String, Object> queryMap) {

		return unifiedAreaCityMapperEx.findAll(queryMap);
	}

	@Transactional
	@Override
	public void delItemTran(Long areaItemId) {

		unifiedAreaCityTargetMapperEx.deleteByAreaCityId(areaItemId);
		unifiedAreaCityVirtMapperEx.deleteByAreaCityId(areaItemId);
		unifiedAreaCityMapper.deleteByPrimaryKey(areaItemId);
	}

	@Override
	public List<SelectOptionAreaItem> findAreaItemSelect(Map<String, Object> params) {

		return unifiedAreaCityMapperEx.findAreaItemSelect(params);
	}

	@Override
	public List<UnifiedAreaTJ> getareatj(Map<String, Object> params) {

		return unifiedAreaCityMapperEx.getareatj(params);
	}

	@Override
	public UnifiedAreaCityWithVirt getAreaDetailByVirtId(Long virtId) {
		return unifiedAreaCityMapperEx.getAreaDetailByVirtId(virtId);
	}

	@Override
	public List<UnifiedAreaWeb> getweb(Map<String, Object> params) {
		return unifiedAreaCityMapperEx.getweb(params);
	}

	@Transactional
	@Override
	public void addObjTran(UnifiedAreaCity unifiedAreaCity) {

		unifiedAreaCityMapper.insert(unifiedAreaCity);

		Long   tempPid   = unifiedAreaCity.getParentId();
		String fullNames = unifiedAreaCity.getAreaName();
		String fullIds   = unifiedAreaCity.getId().toString();

		while (tempPid != null) {
			UnifiedAreaCity item = unifiedAreaCityMapper.selectByPrimaryKey(tempPid);
			fullIds = item.getId() + "," + fullIds;
			fullNames = item.getAreaName() + "," + fullNames;
			tempPid = item.getParentId();
		}

		unifiedAreaCity.setAreaFullNames(fullNames);
		unifiedAreaCity.setAreaFullIds(fullIds);
		unifiedAreaCityMapper.updateByPrimaryKey(unifiedAreaCity);
	}

	@Transactional
	@Override
	public void updateObjTran(UnifiedAreaCity unifiedAreaCity) {

		unifiedAreaCityMapper.updateByPrimaryKeySelective(unifiedAreaCity);

		Long   tempPid   = unifiedAreaCity.getParentId();
		String fullNames = unifiedAreaCity.getAreaName();
		String fullIds   = unifiedAreaCity.getId().toString();

		while (tempPid != null) {
			UnifiedAreaCity item = unifiedAreaCityMapper.selectByPrimaryKey(tempPid);
			fullIds = item.getId() + "," + fullIds;
			fullNames = item.getAreaName() + "," + fullNames;
			tempPid = item.getParentId();
		}

		unifiedAreaCity.setAreaFullNames(fullNames);
		unifiedAreaCity.setAreaFullIds(fullIds);
		unifiedAreaCityMapper.updateByPrimaryKeySelective(unifiedAreaCity);
	}

}
