package cn.common.service.impl;

import cn.common.common.constants.BaseConstant;
import cn.common.common.utils.collection.CollectionUtils;
import cn.common.common.utils.common.CheckParam;
import cn.common.interaction.helper.PageBuilder;
import cn.common.interaction.req.dict.BusinessDictAddReq;
import cn.common.interaction.req.dict.BusinessDictPageReq;
import cn.common.interaction.req.dict.BusinessDictUpdateReq;
import cn.common.interaction.resp.dict.PlatformDictResp;
import cn.common.interaction.resp.page.Pagination;
import cn.common.repository.entity.PlatformDict;
import cn.common.repository.repository.PlatformDictRepository;
import cn.common.service.PlatformDictService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Singer create by singer email:singer-coder@qq.com
 * @projectName health-platform-api
 * @Description: 系统字典Service
 * @date 2019-08-28
 */
@Service(value = "platformDictService")
@Slf4j
public class PlatformDictServiceImpl implements PlatformDictService {

    @Resource
    private BaseConstant baseConstant;

    @Resource
    private PlatformDictRepository platformDictRepository;

    @Resource
    private MapperFacade mapperFacade;

    /**
     * 根据系统业务字典主键ID系统字典信息
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2020-07-07
     * @param platformDictIdList 系统字典主键ID集合
     * @return
     */
    @Override
    public void deleteByMainId(List<String> platformDictIdList){
        log.info(">>>>>>>>>>>>>系统业务字典删除参数Req<<<<<<<<<<<<<<<"+JSON.toJSONString(platformDictIdList));
        if (CollectionUtils.isEmpty(platformDictIdList)) {
            return;
        }
        platformDictRepository.updateDeleteStatusByIdListIn(baseConstant.getDeleteStatus(),platformDictIdList);
    }

    /**
     * 系统字典更新
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2020-07-07
     * @param updateReq 系统字典更新Req
     */
    @Override
    public void updateItem(BusinessDictUpdateReq updateReq){
        log.info(">>>>>>>>>>>>>更新系统业务字典参数Req : {} <<<<<<<<<<<<<<<",
                JSON.toJSONString(updateReq));
        PlatformDict platformDict = platformDictRepository.selectOne(new LambdaQueryWrapper<PlatformDict>()
                .eq(PlatformDict::getPlatformDictId, updateReq.getPlatformDictId()));
        if (CheckParam.isNull(platformDict)) {
            return;
        }
        if(!CheckParam.isNull(updateReq.getDictKey())){
            platformDict.setDictKey(updateReq.getDictKey());
        }
        if(!CheckParam.isNull(updateReq.getDictValue())){
            platformDict.setDictValue(updateReq.getDictValue());
        }
        if(!CheckParam.isNull(updateReq.getDictType())){
            platformDict.setDictType(updateReq.getDictType());
        }
        if(!CheckParam.isNull(updateReq.getDictRemark())){
            platformDict.setDictRemark(updateReq.getDictRemark());
        }
        if(!CheckParam.isNull(updateReq.getSortIndex())){
            platformDict.setSortIndex(updateReq.getSortIndex());
        }
        platformDictRepository.updateById(platformDict);
    }

    /**
     * 分页查询系统业务字典信息
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2020-07-07
     * @param pageReq 分页查询系统业务字典Req
     * @return Pagination
     */
    @Override
    public Pagination<PlatformDictResp> queryByPage(
            BusinessDictPageReq pageReq){
        log.info(">>>>>>>>>>>>>系统业务字典分页查询参数Req : {} <<<<<<<<<<<<<<<",
                JSON.toJSONString(pageReq));
        //构建查询条件
        LambdaQueryWrapper<PlatformDict> queryWrapper = new LambdaQueryWrapper<>();
        if(!CheckParam.isNull(pageReq.getDictKey())){
            queryWrapper.eq(PlatformDict::getDictKey,pageReq.getDictKey());
        }
        if(!CheckParam.isNull(pageReq.getDictType())){
            queryWrapper.eq(PlatformDict::getDictType,pageReq.getDictType());
        }
        if(!CheckParam.isNull(pageReq.getDictValue())){
            queryWrapper.eq(PlatformDict::getDictValue,pageReq.getDictValue());
        }
        if(!CheckParam.isNull(pageReq.getSortIndex())){
            queryWrapper.eq(PlatformDict::getSortIndex,pageReq.getSortIndex());
        }
        queryWrapper.orderByDesc(PlatformDict::getCreateTime);
        //开始分页
        Page<Object> page = PageHelper.startPage(pageReq.getCurrentPage(), pageReq.getItemsPerPage());
        List<PlatformDict> dictionaryList = platformDictRepository.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(dictionaryList)) {
            return PageBuilder.buildPageResult(page,dictionaryList);
        }
        List<PlatformDictResp> respList =
                mapperFacade.mapAsList(dictionaryList, PlatformDictResp.class);
        return PageBuilder.buildPageResult(page,respList);
    }

    /**
     * 根据字典值模糊查询字典信息
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2021/1/21
     * @param dictValue 字段值
     * @return List
     */
    @Override
    public List<PlatformDictResp> queryDictionaryByValue(String dictValue){
        List<PlatformDict> dictionaryList =
                platformDictRepository.selectList(new LambdaQueryWrapper<PlatformDict>()
                        .like(PlatformDict::getDictValue, dictValue));

        if (CollectionUtils.isEmpty(dictionaryList)) {
            return Lists.newArrayList();
        }
        return mapperFacade.mapAsList(dictionaryList, PlatformDictResp.class);
    }

    /**
     * 根据字典分类查询字典信息
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2021/1/21
     * @param dictType 字段类型
     * @return List
     */
    @Override
    public List<PlatformDictResp> queryDictionaryByType(String dictType){
        List<PlatformDict> dictionaryList =
                platformDictRepository.selectList(new LambdaQueryWrapper<PlatformDict>()
                        .eq(PlatformDict::getDictType, dictType));
        if (CollectionUtils.isEmpty(dictionaryList)) {
            return Lists.newArrayList();
        }
        return mapperFacade.mapAsList(dictionaryList, PlatformDictResp.class);
    }

    /**
     * 新增系统字典参数
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2020-07-07
     * @param addReq 新增系统字典参数请求Req
     * @return
     */
    @Override
    public void addItem(BusinessDictAddReq addReq){
        log.info(">>>>>>>>>>>>>新增系统业务字典参数Req : {} <<<<<<<<<<<<<<<",
                JSON.toJSONString(addReq));
        PlatformDict platformDict = new PlatformDict();
        if(!CheckParam.isNull(addReq.getPlatformDictId())){
            platformDict.setPlatformDictId(addReq.getPlatformDictId());
        }
        platformDict.setDictType(addReq.getDictType());
        platformDict.setDictValue(addReq.getDictValue());
        platformDict.setDictKey(addReq.getDictKey());
        platformDict.setDictRemark(addReq.getDictRemark());
        platformDict.setSortIndex(addReq.getSortIndex());
        platformDictRepository.insert(platformDict);
    }

}
