package ink.wgink.module.dictionary.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.module.dictionary.dao.IAreaDao;
import ink.wgink.module.dictionary.pojo.dtos.AreaDTO;
import ink.wgink.module.dictionary.pojo.vos.AreaVO;
import ink.wgink.module.dictionary.service.IAreaService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.ZTreeDTO;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.map.HashMapUtil;
import ink.wgink.util.string.WStringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: AreaServiceImpl
 * @Description: 字典
 * @Author: WangGeng
 * @Date: 2019/11/18 14:09
 * @Version: 1.0
 **/
@Service
public class AreaServiceImpl extends DefaultBaseService implements IAreaService {

    @Autowired
    private IAreaDao areaDao;

    @Override
    public void save(AreaVO areaVO) {
        saveReturnId(areaVO);
    }

    @Override
    public String saveReturnId(AreaVO areaVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(areaVO);
        setSaveInfo(params);
        areaDao.save(params);
        return params.get("areaId").toString();
    }

    @Override
    public void remove(List<String> ids) {
        Map<String, Object> params = getHashMap(3);
        params.put("areaIds", ids);
        setUpdateInfo(params);
        areaDao.remove(params);
    }

    @Override
    public void update(String areaId, AreaVO areaVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(areaVO);
        params.put("areaId", areaId);
        setUpdateInfo(params);
        areaDao.update(params);
    }

    @Override
    public AreaDTO get(String areaId) {
        Map<String, Object> params = super.getHashMap(1);
        params.put("areaId", areaId);
        return areaDao.get(params);
    }

    @Override
    public AreaDTO getByCode(String areaCode) {
        Map<String, Object> params = getHashMap(2);
        params.put("areaCode", areaCode);
        return areaDao.get(params);
    }

    @Override
    public List<AreaDTO> list() {
        Map<String, Object> params = getHashMap(0);
        return areaDao.list(params);
    }

    @Override
    public List<AreaDTO> listByParentId(String areaParentId) {
        Map<String, Object> params = getHashMap(1);
        params.put("areaParentId", areaParentId);
        return areaDao.list(params);
    }

    @Override
    public List<AreaDTO> listAllByParentId(String areaParentId) {
        Map<String, Object> params = getHashMap(1);
        params.put("areaParentId", areaParentId);
        List<AreaDTO> dataAreaDTOs = areaDao.list(params);
        listSub(dataAreaDTOs, params);
        return dataAreaDTOs;
    }

    @Override
    public SuccessResultList<List<AreaDTO>> listPage(ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<AreaDTO> dataAreaDTOs = areaDao.list(page.getParams());
        PageInfo<AreaDTO> pageInfo = new PageInfo<>(dataAreaDTOs);
        return new SuccessResultList<>(dataAreaDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public List<ZTreeDTO> listZTree(Map<String, Object> params) {
        List<ZTreeDTO> zTreeDTOs = areaDao.listZTree(params);
        for (ZTreeDTO zTreeDTO : zTreeDTOs) {
            Integer subCount = areaDao.countByParentId(zTreeDTO.getId());
            setZTreeInfo(zTreeDTO, subCount);
        }
        return zTreeDTOs;
    }

    @Override
    public List<AreaDTO> listByCode(String areaCode) {
        Map<String, Object> params = getHashMap(1);
        params.put("areaCode", WStringUtil.cutContinuityRepeatCharDesc(areaCode, '0'));
        return areaDao.list(params);
    }

    @Override
    public List<AreaDTO> listSubByCode(String areaCode) {
        Map<String, Object> params = getHashMap(4);
        params.put("areaCode", WStringUtil.cutContinuityRepeatCharDesc(areaCode, '0'));
        params.put("excludeAreaCode", areaCode);
        return areaDao.list(params);
    }

    /**
     * 递归查询子组
     *
     * @param dataAreaDTOs
     * @param params
     */
    private void listSub(List<AreaDTO> dataAreaDTOs, Map<String, Object> params) {
        for (AreaDTO dataAreaDTO : dataAreaDTOs) {
            params.put("areaParentId", dataAreaDTO.getAreaId());
            List<AreaDTO> subAreaDTOS = areaDao.list(params);
            dataAreaDTO.setSubArea(subAreaDTOS);
            listSub(subAreaDTOS, params);
        }
    }
}
