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.interfaces.consts.ISystemConstant;
import ink.wgink.module.dictionary.dao.IDataDao;
import ink.wgink.module.dictionary.pojo.dtos.DataDTO;
import ink.wgink.module.dictionary.pojo.vos.DataVO;
import ink.wgink.module.dictionary.service.IDataService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.ZTreeDTO;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.map.HashMapUtil;
import org.apache.commons.lang3.StringUtils;
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: DictionaryServiceImpl
 * @Description: 字典
 * @Author: WangGeng
 * @Date: 2019/11/18 14:09
 * @Version: 1.0
 **/
@Service
public class DataServiceImpl extends DefaultBaseService implements IDataService {

    @Autowired
    private IDataDao dataDao;

    @Override
    public void save(DataVO dataVO) {
        saveReturnId(dataVO);
    }

    @Override
    public String saveReturnId(DataVO dataVO) {
        String uuid = UUIDUtil.getUUID();
        String parentCode = null;
        String dataParentId = dataVO.getDataParentId();
        if (!StringUtils.equals(dataParentId, ISystemConstant.TREE_BASE_ROOT_ID_VALUE)) {
            DataDTO dataDTO = get(dataParentId);
            parentCode = dataDTO.getDataCode();
        }
        String dataCode = getCode(parentCode, dataParentId);
        Map<String, Object> params = HashMapUtil.beanToMap(dataVO);
        params.put("dataCode", dataCode);
        params.put("dataId", uuid);
        setSaveInfo(params);
        dataDao.save(params);
        return uuid;
    }

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

    @Override
    public void update(String dataId, DataVO dataVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(dataVO);
        params.put("dataId", dataId);
        setUpdateInfo(params);
        dataDao.update(params);
    }

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

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

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

    @Override
    public List<DataDTO> listAllByParentId(String dataParentId) {
        Map<String, Object> params = getHashMap(1);
        params.put("dataParentId", dataParentId);
        List<DataDTO> dataDTOs = dataDao.list(params);
        listSub(dataDTOs, params);
        return dataDTOs;
    }

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

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

    @Override
    public List<DataDTO> listByCode(String dataCode) {
        Map<String, Object> params = getHashMap(1);
        params.put("dataCode", dataCode);
        return dataDao.list(params);
    }

    /**
     * 递归查询子组
     *
     * @param dataDTOs
     * @param params
     */
    private void listSub(List<DataDTO> dataDTOs, Map<String, Object> params) {
        for (DataDTO dataDTO : dataDTOs) {
            params.put("dataParentId", dataDTO.getDataId());
            List<DataDTO> subDataDTOs = dataDao.list(params);
            dataDTO.setSubData(subDataDTOs);
            listSub(subDataDTOs, params);
        }
    }

    /**
     * 获取code
     *
     * @param parentCode
     * @param parentId
     * @return
     */
    private String getCode(String parentCode, String parentId) {
        DataDTO dataDTO = dataDao.getLastByParentId(parentId);
        String code = dataDTO != null ? dataDTO.getDataCode() : "0000";
        return super.getNewCode(code, parentCode);
    }
}
