package com.sckw.system.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.exception.SystemException;
import com.sckw.core.model.constant.Global;
import com.sckw.core.model.page.PageHelperUtil;
import com.sckw.core.model.page.PageResult;
import com.sckw.core.utils.BeanUtils;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.StringUtils;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.system.api.model.dto.res.SysDictGroupResDto;
import com.sckw.system.dao.SysDictDao;
import com.sckw.system.dao.SysDictTypeDao;
import com.sckw.system.dubbo.RemoteSystemServiceImpl;
import com.sckw.system.model.SysDict;
import com.sckw.system.model.SysDictType;
import com.sckw.system.model.vo.req.QueryDictTypePageReqVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 字典service接口
 *
 * @author zk
 * @date 2023-05-30
 */
@Service
public class SysDictService {

    @Autowired
    SysDictTypeDao sysDictTypeDao;

    @Autowired
    SysDictDao sysDictDao;

    @Autowired
    RemoteSystemServiceImpl remoteSystemService;

    /**
     * @param params 实体
     * @desc: 添加新纪录
     * @author: czh
     * @date: 2023/7/7
     */
    public void addType(SysDictType params) {
        checkType(params.getType());
        if (sysDictTypeDao.insert(params) <= 0) {
            throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.INSERT_FAIL);
        }
    }

    /**
     * @param type 字典类型
     * @desc: 字典类型查重
     * @author: czh
     * @date: 2023/7/7
     */
    private void checkType(String type) {
        SysDictType sysDictType = new SysDictType();
        sysDictType.setType(type);
        List<SysDictType> list = sysDictTypeDao.select(sysDictType);
        if (!CollectionUtils.isEmpty(list)) {
            throw new SystemException(HttpStatus.QUERY_FAIL_CODE, HttpStatus.DICTTYPE_EXISTS);
        }
    }

    /**
     * @param params 实体
     * @desc: 更新记录
     * @author: czh
     * @date: 2023/7/7
     */
    public void updateType(SysDictType params) {
        checkType(params.getType());
        if (sysDictTypeDao.update(params) <= 0) {
            throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.UPDATE_FAIL);
        }
    }

    /**
     * @param params map

     * @return SysDict
     */
    public List<SysDict> findPage(Map<String, Object> params) {
        return sysDictDao.findPage(params);
    }

    /**
     * @param params 实体
     * @desc: 添加新纪录
     */
    public void add(SysDict params) {
        checkDict(params.getDictId(), params.getValue(), params.getLabel(), params.getParentId());
        if (sysDictDao.insert(params) <= 0) {
            throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.INSERT_FAIL);
        }
    }

    /**
     * @param dictTypeId 字典类型id, value选项值  label
     * @desc: 字典查重
     * @author: czh
     * @date: 2023/7/7
     */
    public void checkDict(Long dictTypeId, String value, String label, String parentId) {
        SysDict sysDict = sysDictDao.queryUnitDict(dictTypeId, value, label, parentId);
        if (!Objects.isNull(sysDict)) {
            throw new SystemException(HttpStatus.QUERY_FAIL_CODE, HttpStatus.DICT_EXISTS);
        }
    }

    /**
     * @param ids 主键
     * @desc: 删除记录-根据主键
     * @author: czh
     * @date: 2023/7/7
     */
    public void deleteByKey(String ids) {
        /*1.数据校验*/
        if (StringUtils.isBlank(ids)) {
            throw new SystemException(HttpStatus.QUERY_FAIL_CODE, HttpStatus.ID_MISSING);
        }

        String[] idArray = ids.split(",");
        for (String id : idArray) {
            SysDict sysDict = sysDictDao.selectByKey(Long.parseLong(id));
            if (null != sysDict) {
                sysDict.setDelFlag(Global.YES);
                if (sysDictDao.update(sysDict) <= 0) {
                    throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.DELETE_FAIL);
                }
            }
        }
    }

    /**
     * @param params 实体
     * @desc: 更新记录
     * @author: czh
     * @date: 2023/7/7
     */
    public void update(SysDict params) {
        checkDict(params.getDictId(), params.getValue(), params.getLabel(), params.getParentId());
        if (sysDictDao.update(params) <= 0) {
            throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.UPDATE_FAIL);
        }
    }

    /**
     *
     *
     * @param key 主键
     * @desc: 根据主键查询
     * @return SysDict
     * @author: czh
     * @date: 2023/7/7
     */
    public SysDict selectByKey(Long key) {
        return sysDictDao.selectByKey(key);
    }

    /**
     * 查询
     * @param params map
     * @return List
     */
    public List<Map<String, Object>> findList(Map<String, Object> params) {
        return sysDictDao.findList(params);
    }

    /**
     * @param reqVo 入参
     * @return PageResult
     * @desc: 查字典类型
     * @author: czh
     * @date: 2023/7/6
     */
    public PageResult queryDictTypePage(QueryDictTypePageReqVo reqVo) {
        PageHelper.startPage(reqVo.getPage(), reqVo.getPageSize());
        List<SysDictType> list = queryDictType(reqVo);
        return PageHelperUtil.getPageResult(new PageInfo<>(list));
    }

    public List<SysDictType> queryDictType(QueryDictTypePageReqVo reqVo) {
        SysDictType sysDictType = new SysDictType();
        BeanUtils.copyProperties(reqVo, sysDictType);
        return sysDictTypeDao.select(sysDictType);
    }

    public List<SysDictGroupResDto> findDictTree(String type, String value) {
        if (StringUtils.isBlank(value)) {
            return remoteSystemService.queryDictGroupByType(type);
        }

        SysDictGroupResDto sysDictGroupResDto = remoteSystemService.queryDictGroupByTypeAndValue(type, value);
        return Collections.singletonList(sysDictGroupResDto);
    }
}
