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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.enums.ESupplierType;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.system.base.BdCubasdoc;
import com.hzs.common.domain.system.base.ext.BdCubasdocExt;
import com.hzs.system.base.mapper.BdCubasdocMapper;
import com.hzs.system.base.param.BdCubasdocParam;
import com.hzs.system.base.service.IBdCubasdocService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 供应商设置 服务实现类
 */
@Service
public class BdCubasdocServiceImpl extends ServiceImpl<BdCubasdocMapper, BdCubasdoc> implements IBdCubasdocService {

    @Autowired
    private RedisService redisService;

    @Override
    public List<BdCubasdocExt> querySupplierByCondition(BdCubasdocParam cubasdocParam) {
        List<Integer> custTypeList = new ArrayList<>();
        custTypeList.add(ESupplierType.INNER_SUPPLIER.getValue());
        custTypeList.add(ESupplierType.OUT_SUPPLIER.getValue());
        cubasdocParam.setCustTypeList(custTypeList);
        return queryCubasdocByCondition(cubasdocParam);
    }

    @Override
    public List<BdCubasdocExt> queryCustomerByCondition(BdCubasdocParam cubasdocParam) {
        List<Integer> custTypeList = new ArrayList<>();
        custTypeList.add(ESupplierType.INNER_CUSTOMER.getValue());
        custTypeList.add(ESupplierType.OUT_CUSTOMER.getValue());
        cubasdocParam.setCustTypeList(custTypeList);
        return queryCubasdocByCondition(cubasdocParam);
    }

    @Override
    public List<BdCubasdocExt> queryCubasdocByCondition(BdCubasdocParam cubasdocParam) {
//        QueryWrapper<BdCubasdoc> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq(SystemFieldConstants.PK_COUNTRY, cubasdocParam.getPkCountry());
//        if(CollectionUtil.isNotEmpty(cubasdocParam.getCustTypeList())){
//            queryWrapper.in("CUST_TYPE", cubasdocParam.getCustTypeList());
//        }
//        if(cubasdocParam.getPkId() != null){
//            queryWrapper.eq(SystemFieldConstants.PK_ID, cubasdocParam.getPkId());
//        }
        return baseMapper.queryCubasdocByCondition(cubasdocParam);
    }

    @Override
    public void saveCubasdoc(BdCubasdocParam cubasdocParam, Long pkMember) {
        if (cubasdocParam.getPkId() == null) {
            BdCubasdoc bdCubasdoc = BeanUtil.copyProperties(cubasdocParam, BdCubasdoc.class);
            bdCubasdoc.setPkCreator(pkMember);
            baseMapper.insert(bdCubasdoc);
        } else {
            BdCubasdoc bdCubasdoc = baseMapper.selectById(cubasdocParam.getPkId());
            bdCubasdoc.setCustName(cubasdocParam.getCustName());
            bdCubasdoc.setCustType(cubasdocParam.getCustType());
            bdCubasdoc.setShortName(cubasdocParam.getShortName());
            bdCubasdoc.setLegalBody(cubasdocParam.getLegalBody());
            bdCubasdoc.setLinkMan(cubasdocParam.getLinkMan());
            bdCubasdoc.setTaxpayerid(cubasdocParam.getTaxpayerid());
            bdCubasdoc.setProvince(cubasdocParam.getProvince());
            bdCubasdoc.setCity(cubasdocParam.getCity());
            bdCubasdoc.setCounty(cubasdocParam.getCounty());
            bdCubasdoc.setAddress(cubasdocParam.getAddress());
            bdCubasdoc.setTradeScope(cubasdocParam.getTradeScope());
            bdCubasdoc.setPkModified(pkMember);
            bdCubasdoc.setModifiedTime(DateUtils.currentDateTime());
            baseMapper.updateById(bdCubasdoc);
            flushCubasdoc(cubasdocParam.getPkId());
        }
    }

    @Override
    public void deleteCuBasdoc(Integer pkId, Long pkMember) {
        UpdateWrapper<BdCubasdoc> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set(SystemFieldConstants.DEL_FLAG, EYesNo.NO.getIntValue());
        updateWrapper.set(SystemFieldConstants.PK_MODIFIED, pkMember);
        updateWrapper.set(SystemFieldConstants.MODIFIED_TIME, DateUtils.currentDateTime());
        updateWrapper.eq(SystemFieldConstants.PK_ID, pkId);
        baseMapper.delete(updateWrapper);
        flushCubasdoc(pkId);
    }

    /*
     *  刷新缓存
     **/
    private void flushCubasdoc(Integer pkId) {
        BdCubasdoc cubasdoc = redisService.getCacheObject(CacheConstants.CUSTOMER_ID + pkId);
        if (cubasdoc != null) {
            redisService.deleteObjectByKey(CacheConstants.CUSTOMER_ID + cubasdoc);
        }
    }
}
