package com.hzs.system.config.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.enums.EAgreementWhiteType;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.domain.system.config.BdAgreementRenewalWhite;
import com.hzs.common.domain.system.config.ext.BdAgreementRenewalWhiteExt;
import com.hzs.system.config.mapper.BdAgreementRenewalWhiteMapper;
import com.hzs.system.config.service.IBdAgreementRenewalWhiteService;
import com.hzs.system.config.vo.BdAgreementRenewalWhiteVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 协议续约白名单 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2024-01-12
 */
@Service
public class BdAgreementRenewalWhiteServiceImpl extends ServiceImpl<BdAgreementRenewalWhiteMapper, BdAgreementRenewalWhite> implements IBdAgreementRenewalWhiteService {


    @Override
    public List<BdAgreementRenewalWhite> selectByPkMember(Long pkMember) {
        QueryWrapper<BdAgreementRenewalWhite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_MEMBER", pkMember);
        return this.list(queryWrapper);

    }

    @Override
    public BdAgreementRenewalWhite selectByPkMemberAndType(Long pkMember, Integer type) {
        List<Integer> typeList = Arrays.asList(EAgreementWhiteType.AUTO_RENEW.getValue(),type);
        QueryWrapper<BdAgreementRenewalWhite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_MEMBER", pkMember);
        queryWrapper.in("TYPE", typeList);
        return this.getOne(queryWrapper);

    }

    @Override
    public Object[] selectWhiteList(BdAgreementRenewalWhiteVO agreementRenewalWhiteVO) {
        List<BdAgreementRenewalWhiteExt> bdAgreementRenewalWhiteExts = baseMapper.selectWhiteList(agreementRenewalWhiteVO);
        List<BdAgreementRenewalWhiteVO> agreementRenewalWhiteVOList = BeanUtil.copyToList(bdAgreementRenewalWhiteExts, BdAgreementRenewalWhiteVO.class);
        return new Object[]{bdAgreementRenewalWhiteExts, agreementRenewalWhiteVOList};
    }

    @Override
    public Boolean deleteAgreementRenewalWhite(String pkId, Long pkCreator) {
        List<Integer> pkIdList = Arrays.stream(pkId.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        UpdateWrapper<BdAgreementRenewalWhite> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set(SystemFieldConstants.DEL_FLAG, EYesNo.NO.getIntValue());
        updateWrapper.set(SystemFieldConstants.PK_MODIFIED, pkCreator);
        updateWrapper.set(SystemFieldConstants.MODIFIED_TIME, new Date());
        updateWrapper.in(SystemFieldConstants.PK_ID, pkIdList);
        return update(updateWrapper);
    }


    @Override
    public Integer isIncludeMemberId(@Param("memberPkIds") List<Long> memberPkIds, @Param("pkCountry") Integer pkCountry) {
        return baseMapper.isIncludeMemberId(memberPkIds, pkCountry);
    }


    @Override
    public Integer selectAgreementWhiteByPkMember(Long pkMember) {
        return baseMapper.selectAgreementWhiteByPkMember(pkMember);
    }
}
