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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.ESupplierType;
import com.hzs.common.core.enums.EYesNo;
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.stereotype.Service;

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

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

    @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> querySupplierByProduct(BdCubasdocParam cubasdocParam) {
        List<Integer> custTypeList = new ArrayList<>();
        custTypeList.add(ESupplierType.INNER_SUPPLIER.getValue());
        custTypeList.add(ESupplierType.OUT_SUPPLIER.getValue());
        cubasdocParam.setCustTypeList(custTypeList);
        return baseMapper.querySupplierByProduct(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);
            // 供应商编号自动生成
            if (cubasdocParam.getCustCode() == null) {
                if (cubasdocParam.getCustType() == 1 || cubasdocParam.getCustType() == 2) {
                    bdCubasdoc.setCustCode(createCuBasDocCode("SU"));
                } else {
                    bdCubasdoc.setCustCode(createCuBasDocCode("CU"));
                }
            }
            bdCubasdoc.setPkSupplier(cubasdocParam.getPkSupplier());
            bdCubasdoc.setPkCreator(pkMember);
            bdCubasdoc.setPkUser(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());
            bdCubasdoc.setPkSupplier(cubasdocParam.getPkId());
            baseMapper.updateById(bdCubasdoc);
        }
    }

    private String createCuBasDocCode(String prefix) {
        // 生成loginName
        long next = (long) (Math.random() * (9999999L - 1000000L + 1) + 1000000L);
        String basDocCode = prefix + next;
        // 2023.10.19 需求加校验，会员编号不能出现 4 和 0
        if (basDocCode.contains("4") || basDocCode.contains("0")) {
            return createCuBasDocCode(prefix);
        }
        BdCubasdoc bdCubasdoc = queryBdCuBasdoc(basDocCode);
        if (bdCubasdoc != null) {
            return createCuBasDocCode(prefix);
        }
        return basDocCode;
    }

    private BdCubasdoc queryBdCuBasdoc(String custCode) {
        LambdaQueryWrapper<BdCubasdoc> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdCubasdoc::getCustCode, custCode);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public void deleteCuBasdoc(Integer pkId, Long pkMember) {
        LambdaUpdateWrapper<BdCubasdoc> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(BdCubasdoc::getDelFlag, EYesNo.NO.getIntValue());
        updateWrapper.set(BdCubasdoc::getPkModified, pkMember);
        updateWrapper.set(BdCubasdoc::getModifiedTime, DateUtils.currentDateTime());
        updateWrapper.eq(BdCubasdoc::getPkId, pkId);
        baseMapper.delete(updateWrapper);
    }

    @Override
    public Integer queryThSupplierApplyUserList(Long userId) {
        return baseMapper.queryThSupplierApplyUserList(userId);
    }

}
