package com.hzs.ground.supplier.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.enums.*;
import com.hzs.common.domain.ground.supplier.ThSupplierApply;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.ground.supplier.mapper.ThSupplierApplyMapper;
import com.hzs.ground.supplier.service.IThSupplierApplyService;
import com.hzs.ground.supplier.vo.ThSupplierApplyVO;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 供应商申请 服务实现类
 *
 * @author hzs
 * @since 2024-03-21
 */
@Service
public class ThSupplierApplyServiceImpl extends ServiceImpl<ThSupplierApplyMapper, ThSupplierApply> implements IThSupplierApplyService {

    /**
     * @description: 供应商列表
     * @author: zhang jing
     * @date: 2024/3/22 11:31
     * @param: [thSupplierApplyVO]
     * @return: java.util.List<com.hzs.ground.supplier.vo.ThSupplierApplyVO>
     **/
    @Override
    public List<ThSupplierApplyVO> queryThSupplierApplyList(ThSupplierApplyVO thSupplierApplyVO) {
        return baseMapper.queryThSupplierApplyList(thSupplierApplyVO);
    }

    /**
     * @description: 查询登录管理员是否为供应商
     * @author: zhang jing
     * @date: 2024/4/19 16:40
     * @param: [userId]
     * @return: java.lang.Integer
     **/
    @Override
    public Integer queryThSupplierApplyUserList(Long userId) {
        return baseMapper.queryThSupplierApplyUserList(userId);
    }


    /**
     * @description: 添加供应商申请
     * @author: zhang jing
     * @date: 2024/3/22 11:52
     * @param: [thSupplierApplyVO]
     * @return: boolean
     **/
    @Override
    public boolean saveThSupplierApply(ThSupplierApplyVO thSupplierApplyVO) {
        ThSupplierApply tsa = BeanUtil.copyProperties(thSupplierApplyVO, ThSupplierApply.class);
        List<String> fileList = thSupplierApplyVO.getFileList();
        int maxAptitudeCount = Math.min(fileList.size(), 8);
        for (int i = 0; i < maxAptitudeCount; i++) {
            switch (i) {
                case 0:
                    tsa.setAptitude1(fileList.get(0));
                    break;
                case 1:
                    tsa.setAptitude2(fileList.get(1));
                    break;
                case 2:
                    tsa.setAptitude3(fileList.get(2));
                    break;
                case 3:
                    tsa.setAptitude4(fileList.get(3));
                    break;
                case 4:
                    tsa.setAptitude5(fileList.get(4));
                    break;
                case 5:
                    tsa.setAptitude6(fileList.get(5));
                    break;
                case 6:
                    tsa.setAptitude7(fileList.get(6));
                    break;
                case 7:
                    tsa.setAptitude8(fileList.get(7));
                    break;
                default:
                    break;
            }
        }
        baseMapper.insert(tsa);
        return true;
    }

    /**
     * @description: 修改供应商申请
     * @author: zhang jing
     * @date: 2024/3/22 11:54
     * @param: [thSupplierApplyVO]
     * @return: boolean
     **/
    @Override
    public boolean updateThSupplierApply(ThSupplierApplyVO thSupplierApplyVO) {
        ThSupplierApply tsa = BeanUtil.copyProperties(thSupplierApplyVO, ThSupplierApply.class);
        List<String> fileList = thSupplierApplyVO.getFileList();
        int maxAptitudeCount = Math.min(fileList.size(), 8);
        for (int i = 0; i < maxAptitudeCount; i++) {
            switch (i) {
                case 0:
                    tsa.setAptitude1(fileList.get(0));
                    break;
                case 1:
                    tsa.setAptitude2(fileList.get(1));
                    break;
                case 2:
                    tsa.setAptitude3(fileList.get(2));
                    break;
                case 3:
                    tsa.setAptitude4(fileList.get(3));
                    break;
                case 4:
                    tsa.setAptitude5(fileList.get(4));
                    break;
                case 5:
                    tsa.setAptitude6(fileList.get(5));
                    break;
                case 6:
                    tsa.setAptitude7(fileList.get(6));
                    break;
                case 7:
                    tsa.setAptitude8(fileList.get(7));
                    break;
                default:
                    break;
            }
        }
        baseMapper.updateById(tsa);
        return true;
    }

    /**
     * @description: 删除供应商申请
     * @author: zhang jing
     * @date: 2024/3/22 13:49
     * @param: [pkId]
     * @return: boolean
     **/
    @Override
    public boolean delThSupplierApply(Long pkId) {
        UpdateWrapper<ThSupplierApply> updateWrapper = new UpdateWrapper();
        updateWrapper.eq("PK_ID", pkId);
        updateWrapper.set("PK_MODIFIED", SecurityUtils.getUserId());
        updateWrapper.set("MODIFIED_TIME", new Date());
        updateWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        baseMapper.update(null, updateWrapper);
        return true;
    }

    /**
     * @description: 供应商申请审批查看
     * @author: zhang jing
     * @date: 2024/3/25 14:41
     * @param: [eApprovalBusiness, businessCode, pkCountry]
     * @return: java.util.List<com.hzs.ground.supplier.vo.ThSupplierApplyVO>
     **/
    @Override
    public List<ThSupplierApplyVO> queryThSupplierApplyByCode(String businessCode, Integer pkCountry) {
        return baseMapper.queryThSupplierApplyByCode(businessCode, pkCountry);
    }
}
