package com.friday.custody.service.basicService.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.friday.custody.dao.DataDictionarySellerDao;
import com.friday.custody.entity.DataDictionarySellerEntity;
import com.friday.custody.enums.DataDictionaryTypeEnum;
import com.friday.custody.service.basicService.DataDictionarySellerService;
import com.friday.custody.service.logicService.DataDictionaryBasicService;
import com.friday.custody.utils.PageUtils;
import com.friday.custody.utils.Query;
import com.friday.custody.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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


@Slf4j
@Service("dataDictionarySellerService")
public class DataDictionarySellerServiceImpl extends ServiceImpl<DataDictionarySellerDao, DataDictionarySellerEntity>
        implements DataDictionarySellerService, DataDictionaryBasicService {
    @Autowired
    DataDictionarySellerDao sellerDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DataDictionarySellerEntity> page = this.page(
                new Query<DataDictionarySellerEntity>().getPage(params),
                new QueryWrapper<DataDictionarySellerEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Map<String, String> queryWithDetail(DataDictionaryTypeEnum dicType, String trustNo) {
        Map<String, String> result = new HashMap<>();
        QueryWrapper<DataDictionarySellerEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("origin_value", trustNo);
        List<DataDictionarySellerEntity> allDatas = baseMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(allDatas)) {
            result = allDatas.stream().collect(Collectors.toMap(DataDictionarySellerEntity::getMapValue, DataDictionarySellerEntity::getKeyStr));
        } else {
            log.error("DataDictionarySellerEntity 查询失败！");
        }
        return result;
    }

    @Override
    public Map<String, String> queryWithDetail(String trustNo) {
        Map<String, String> result = new HashMap<>();
        QueryWrapper<DataDictionarySellerEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("origin_value", trustNo);
        List<DataDictionarySellerEntity> allDatas = baseMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(allDatas)) {
                result = allDatas.stream().collect(Collectors.toMap(DataDictionarySellerEntity::getMapValue, DataDictionarySellerEntity::getKeyStr));
        } else {
            log.error("DataDictionarySellerEntity 查询失败！");
        }
        return result;
    }
    @Override
    public R queryData(String type) {
        List<DataDictionarySellerEntity> entities = baseMapper.selectList(new QueryWrapper<>());
        if (!CollectionUtils.isEmpty(entities)) {
            return R.ok("查询字典信息成功！")
                    .put("data", entities.stream().collect(Collectors.toMap(DataDictionarySellerEntity::getKeyStr
                            ,DataDictionarySellerEntity::getMapValue)));
        }
        return R.ok("字典信息为空，请联系管理员配置！");
    }

    public String queryAgencyName(String originValue, String agencyNo) {
        String agencyName = sellerDao.selectAgencyName(originValue, agencyNo);
        log.info("queryAgencyName_originValue={},agencyNo={},agencyName={}",originValue,agencyNo,agencyName);
        return agencyName;
    }

    public String queryAgencyNo(String originValue, String agencyName) {
        String agencyNo = sellerDao.selectAgencyNo(originValue, agencyName);
        log.info("queryAgencyName_originValue={},agencyName={},agencyNo={}",originValue,agencyName,agencyNo);
        return agencyNo;
    }

    public String queryKey(String originValue, String value) {
        return sellerDao.selectKey(originValue,value);
    }
    public String queryValue(String originValue,String keyStr) {
        QueryWrapper<DataDictionarySellerEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("key_str",keyStr);
        queryWrapper.eq("origin_value",originValue);
        DataDictionarySellerEntity dataDictionarySellerEntity = baseMapper.selectOne(queryWrapper);
        String value = dataDictionarySellerEntity.getValue();
        return value;
    }

    /**
     * 保存销售商字典表
     * @param value 映射的值
     * @param mapValue 原值
     * @return insertId 添加的id，如果已经添加了就返回0，未添加返回添加的id
     */

    public Integer saveSellerKey(String value, String mapValue, String originValue){
        //判断是否存在与表,
        QueryWrapper<DataDictionarySellerEntity> existQueryWrapper = new QueryWrapper<>();
        existQueryWrapper.eq("value",value);
        existQueryWrapper.eq("map_value",mapValue);
        existQueryWrapper.eq("origin_value",originValue);
        DataDictionarySellerEntity dictionarySellerEntity = baseMapper.selectOne(existQueryWrapper);
        if (null!=dictionarySellerEntity){
            return 0;
        }
        //获取最后一个key_str
        QueryWrapper<DataDictionarySellerEntity> queryWrapperKeyStr = new QueryWrapper<>();
        queryWrapperKeyStr.select("MAX(CAST(key_str AS UNSIGNED) ) key_str");
        DataDictionarySellerEntity entity = baseMapper.selectOne(queryWrapperKeyStr);
        String keyStr = entity.getKeyStr();

        DataDictionarySellerEntity dataDictionarySellerEntity = new DataDictionarySellerEntity();
        int i = Integer.parseInt(keyStr);
        dataDictionarySellerEntity.setKeyStr(Integer.toString(++i));
        dataDictionarySellerEntity.setValue(value);
        dataDictionarySellerEntity.setOriginValue(originValue);
        dataDictionarySellerEntity.setMapValue(mapValue);
        dataDictionarySellerEntity.setCreateTime(new Date());
        dataDictionarySellerEntity.setUpdateTime(new Date());
        //入库
        baseMapper.insert(dataDictionarySellerEntity);
        Integer id = dataDictionarySellerEntity.getId();
        return id;
    }
}