package com.cm.common.plugin.service.datadictionary.impl;

import com.cm.common.base.AbstractService;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SearchException;
import com.cm.common.plugin.dao.datadictionary.IDataDictionaryDao;
import com.cm.common.plugin.pojo.dtos.datadictionary.DataDictionaryDTO;
import com.cm.common.plugin.pojo.vos.datadictionary.DataDictionaryVO;
import com.cm.common.plugin.service.datadictionary.IDataDictionaryService;
import com.cm.common.pojo.ListPage;
import com.cm.common.pojo.dtos.ZTreeDTO;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultList;
import com.cm.common.utils.HashMapUtil;
import com.cm.common.utils.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
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 DataDictionaryServiceImpl extends AbstractService implements IDataDictionaryService {

    @Autowired
    private IDataDictionaryDao dictionaryDao;

    @Override
    public SuccessResult saveDictionary(DataDictionaryVO dictionaryVO) throws Exception {
        saveDictionaryInfo(null, dictionaryVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult saveDictionary(String token, DataDictionaryVO dictionaryVO) throws Exception {
        saveDictionaryInfo(token, dictionaryVO);
        return new SuccessResult();
    }

    @Override
    public String saveDictionaryReturnId(DataDictionaryVO dictionaryVO) throws Exception {
        return saveDictionaryInfo(null, dictionaryVO);
    }

    @Override
    public String saveDictionaryReturnId(String token, DataDictionaryVO dictionaryVO) throws Exception {
        return saveDictionaryInfo(token, dictionaryVO);
    }

    private String saveDictionaryInfo(String token, DataDictionaryVO dictionaryVO) throws Exception {
        String uuid = UUIDUtil.getUUID();
        String parentCode = null;
        String dictionaryParentId = dictionaryVO.getDictionaryParentId();
        if (!StringUtils.equals(dictionaryParentId, ISystemConstant.TREE_BASE_ROOT_ID_VALUE)) {
            DataDictionaryDTO dictionaryDTO = getDictionaryById(dictionaryParentId);
            parentCode = dictionaryDTO.getDictionaryCode();
        }
        String dictionaryCode = getCode(parentCode, dictionaryParentId);
        Map<String, Object> params = HashMapUtil.beanToMap(dictionaryVO);
        params.put("dictionaryCode", dictionaryCode);
        params.put("dictionaryId", uuid);
        if (StringUtils.isBlank(token)) {
            setSaveInfo(params);
        } else {
            setSaveInfo(token, params);
        }
        dictionaryDao.saveDictionary(params);
        return uuid;
    }

    @Override
    public SuccessResult removeDictionary(String ids) throws RemoveException {
        removeDictionaryInfo(null, ids);
        return new SuccessResult();
    }

    @Override
    public SuccessResult removeDictionary(String token, String ids) throws RemoveException {
        removeDictionaryInfo(token, ids);
        return new SuccessResult();
    }

    private void removeDictionaryInfo(String token, String ids) {
        Map<String, Object> params = getHashMap(3);
        params.put("dictionaryIds", Arrays.asList(ids.split("_")));
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setUpdateInfo(token, params);
        }
        dictionaryDao.removeDictionary(params);
    }

    @Override
    public SuccessResult updateDictionary(String dictionaryId, DataDictionaryVO dictionaryVO) throws Exception {
        updateDictionaryInfo(null, dictionaryId, dictionaryVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult updateDictionary(String token, String dictionaryId, DataDictionaryVO dictionaryVO) throws Exception {
        updateDictionaryInfo(token, dictionaryId, dictionaryVO);
        return new SuccessResult();
    }

    private void updateDictionaryInfo(String token, String dictionaryId, DataDictionaryVO dictionaryVO) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(dictionaryVO);
        params.put("dictionaryId", dictionaryId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setUpdateInfo(token, params);
        }
        dictionaryDao.updateDictionary(params);
    }

    @Override
    public DataDictionaryDTO getDictionaryById(String dictionaryId) throws SearchException {
        Map<String, Object> params = super.getHashMap(1);
        params.put("dictionaryId", dictionaryId);
        return dictionaryDao.getDictionary(params);
    }

    @Override
    public List<DataDictionaryDTO> listDictionaryByParentId(String dictionaryParentId) throws SearchException {
        Map<String, Object> params = getHashMap(1);
        params.put("dictionaryParentId", dictionaryParentId);
        return dictionaryDao.listDictionary(params);
    }

    @Override
    public List<DataDictionaryDTO> listDictionaryAllByParentId(String dictionaryParentId) throws SearchException {
        Map<String, Object> params = getHashMap(1);
        params.put("dictionaryParentId", dictionaryParentId);
        List<DataDictionaryDTO> dictionaryDTOs = dictionaryDao.listDictionary(params);
        listSubDictionarys(dictionaryDTOs, params);
        return dictionaryDTOs;
    }

    @Override
    public SuccessResultList<List<DataDictionaryDTO>> listPageDictionary(ListPage page) throws SearchException {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<DataDictionaryDTO> dictionaryDTOs = dictionaryDao.listDictionary(page.getParams());
        PageInfo<DataDictionaryDTO> pageInfo = new PageInfo<>(dictionaryDTOs);
        return new SuccessResultList<>(dictionaryDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

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

    @Override
    public List<DataDictionaryDTO> listDictionaryByDictionaryCode(String dictionaryCode) throws SearchException {
        Map<String, Object> params = getHashMap(1);
        params.put("dictionaryCode", dictionaryCode);
        return dictionaryDao.listDictionary(params);
    }

    /**
     * 递归查询子组
     *
     * @param dictionaryDTOs
     * @param params
     */
    private void listSubDictionarys(List<DataDictionaryDTO> dictionaryDTOs, Map<String, Object> params) throws SearchException {
        for (DataDictionaryDTO dictionaryDTO : dictionaryDTOs) {
            params.put("dictionaryParentId", dictionaryDTO.getDictionaryId());
            List<DataDictionaryDTO> subDataDictionaryDTOs = dictionaryDao.listDictionary(params);
            dictionaryDTO.setSubDictionary(subDataDictionaryDTOs);
            listSubDictionarys(subDataDictionaryDTOs, params);
        }
    }

    /**
     * 获取code
     *
     * @param parentCode
     * @param parentId
     * @return
     */
    private String getCode(String parentCode, String parentId) throws SearchException {
        DataDictionaryDTO dictionaryDTO = dictionaryDao.getLastByParentId(parentId);
        String code = dictionaryDTO != null ? dictionaryDTO.getDictionaryCode() : "0000";
        return super.getNewCode(code, parentCode);
    }
}
