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.DataDictionarySellerDetailsDao;
import com.friday.custody.entity.DataDictionarySellerDetailsEntity;
import com.friday.custody.service.basicService.DataDictionarySellerDetailsService;
import com.friday.custody.utils.PageUtils;
import com.friday.custody.utils.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("dataDictionarySellerDetailsService")
public class DataDictionarySellerDetailsServiceImpl extends ServiceImpl<DataDictionarySellerDetailsDao, DataDictionarySellerDetailsEntity> implements DataDictionarySellerDetailsService {

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

        return new PageUtils(page);
    }

    @Override
    public List<DataDictionarySellerDetailsEntity> selectVByTrustNo(String trustNo) {
        return baseMapper.selectList(new QueryWrapper<DataDictionarySellerDetailsEntity>().eq("trust_no", trustNo));
    }

    /**
     * 保存销售商字典映射
     * @param dicId 字典id
     * @param trustNo 数据字典：托管人
     * @param originvalue 托管方数据值
     * @return 如果存在返回0，不存在返回添加的id
     */
    public Integer saveSellerDetails(Integer dicId, String trustNo, String originvalue){
        //判断是否存在
        QueryWrapper<DataDictionarySellerDetailsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("trust_no",trustNo);
        queryWrapper.eq("origin_value",originvalue);
        DataDictionarySellerDetailsEntity sellerDetailsEntity = baseMapper.selectOne(queryWrapper);
        if (null!=sellerDetailsEntity){
            return 0;
        }
        DataDictionarySellerDetailsEntity entity = new DataDictionarySellerDetailsEntity();
        entity.setDicId(dicId);
        entity.setTrustNo(trustNo);
        entity.setOriginValue(originvalue);
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        //入库
        baseMapper.insert(entity);
        Integer id = entity.getId();
        return id;
    }
}