package cn.superdesk.uniorder.service.sys.role.impl;


import cn.superdesk.uniorder.common.enums.common.CommonEnabledEnum;
import cn.superdesk.uniorder.common.enums.common.CommonSoftDeleteEnum;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.numerical.AnCollectionUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.sys.role.SysMenuOpenApi;
import cn.superdesk.uniorder.dao.entity.sys.role.SysMenuOpenApiExample;
import cn.superdesk.uniorder.dao.mapper.sys.role.SysMenuOpenApiMapper;
import cn.superdesk.uniorder.dto.param.system.role.SysMenuOpenApiQueryCriteria;
import cn.superdesk.uniorder.service.sys.role.SysMenuOpenApiService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 一级菜单-二级菜单-按钮 X 开放接口
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2021-11-01 10:58:18
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-11-01 10:58:18
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Service
public class SysMenuOpenApiServiceImpl implements SysMenuOpenApiService {

    @Autowired
    private SysMenuOpenApiMapper sysMenuOpenApiMapper;


    /****************************************** extends BaseService 部分 start ******************************************/


    @Override
    public int insertSelective(SysMenuOpenApi entity) {
        return sysMenuOpenApiMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(SysMenuOpenApi entity) {
        return sysMenuOpenApiMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public SysMenuOpenApi findById(Long id) {
        return sysMenuOpenApiMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<SysMenuOpenApi> findByPaging(Map<String, Object> conditions, Integer pageNun, Integer pageSize) {
        return null;
    }

    @Override
    public List<SysMenuOpenApi> findAll(Map<String, Object> queryMap) {

        SysMenuOpenApiExample          example  = new SysMenuOpenApiExample();
        SysMenuOpenApiExample.Criteria criteria = example.createCriteria();

        criteria
                .andDeletedEqualTo(CommonSoftDeleteEnum.正常.getKey())
                .andEnabledEqualTo(CommonEnabledEnum.启用.getKey());

        List<SysMenuOpenApi> resultList = sysMenuOpenApiMapper.selectByExample(example);

        return resultList;
    }


    /****************************************** extends BaseService 部分   end ******************************************/


    public void addSysMenuOpenApi(SysMenuOpenApi entity) {
        sysMenuOpenApiMapper.insertSelective(entity);
    }

    public void updateSysMenuOpenApi(SysMenuOpenApi entity) {
        SysMenuOpenApi originEntity = sysMenuOpenApiMapper.selectByPrimaryKey(entity.getId());
//		AssertUtil.notNull(originEntity);
        AnBeanUtils.copy(entity, originEntity);
        sysMenuOpenApiMapper.updateByPrimaryKey(originEntity);
    }

    public void deleteSysMenuOpenApi(Long id) {
        sysMenuOpenApiMapper.deleteByPrimaryKey(id);
    }

    public SysMenuOpenApi findSysMenuOpenApiById(Long id) {
        SysMenuOpenApi entity = sysMenuOpenApiMapper.selectByPrimaryKey(id);
//		AssertUtil.notNull(entity);
        return entity;
    }

    public R<List<SysMenuOpenApi>> pageQuery(Map<String, Object> conditions, int pageNun, int pageSize) {

        SysMenuOpenApiExample          example  = new SysMenuOpenApiExample();
        SysMenuOpenApiExample.Criteria criteria = example.createCriteria();

        // TODO 喜欢用Map 还是 Bean 自己喜欢... 后期添加 转 SysMenuOpenApiExample.Criteria
        // SysMenuOpenApi sysMenuOpenApiConditions = BeanUtils.mapToBean(conditions,SysMenuOpenApi.class);

        PageHelper.startPage(pageNun, pageSize);
        List<SysMenuOpenApi>     resultList = sysMenuOpenApiMapper.selectByExample(example);
        PageInfo<SysMenuOpenApi> page       = new PageInfo<SysMenuOpenApi>(resultList);
        PageHelper.clearPage();

        return new R<List<SysMenuOpenApi>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), resultList);

    }

    public R<List<SysMenuOpenApi>> pageQueryGenericParam(SysMenuOpenApiQueryCriteria queryCriteria) {

        SysMenuOpenApiExample          example  = new SysMenuOpenApiExample();
        SysMenuOpenApiExample.Criteria criteria = example.createCriteria();

        // TODO 添加条件... 后期添加 queryCriteria 转 SysMenuOpenApiExample.Criteria

        PageHelper.startPage(queryCriteria.getPageNumber(), queryCriteria.getPageSize());
        List<SysMenuOpenApi>     resultList = sysMenuOpenApiMapper.selectByExample(example);
        PageInfo<SysMenuOpenApi> page       = new PageInfo<SysMenuOpenApi>(resultList);
        PageHelper.clearPage();

        return new R<List<SysMenuOpenApi>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), resultList);

    }

    /****************************************** 工具方法 start ******************************************/

    public Map<String, SysMenuOpenApi> getPerm2MapStringEntity() {

        List<SysMenuOpenApi> resultList = findAll(new HashMap<>());

        Map<String, SysMenuOpenApi> result = new HashMap<>();

        if (AnCollectionUtils.isNotEmpty(resultList)) {

//			result = resultList.stream().collect(Collectors.toMap(item -> item.getClazzUrl() + item.getMethodUrl(), v -> v, (k, v) -> v));
            result = resultList.stream().collect(Collectors.toMap(item -> item.getMethodUrl(), v -> v, (k, v) -> v));
        }

        return result;
    }

    public Map<String, String> getPerm2MapStringEntityString() {

        List<SysMenuOpenApi> resultList = findAll(new HashMap<>());

        Map<String, String> result = new HashMap<>();

        if (AnCollectionUtils.isNotEmpty(resultList)) {

//			result = resultList.stream().collect(Collectors.toMap(item -> item.getClazzUrl() + item.getMethodUrl(), v -> JsonUtils.toJson(v), (k, v) -> v));
            result = resultList.stream().collect(Collectors.toMap(item -> item.getMethodUrl(), v -> AnJsonUtils.toJson(v), (k, v) -> v));
        }

        return result;
    }

}