package com.kxs.product.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.common.core.util.R;
import com.kxs.product.api.model.KxsBrand;
import com.kxs.product.api.vo.kxsapp.excel.MachineDeductExcelVo;
import com.kxs.product.biz.constant.enums.ProductErrorTypeEnum;
import com.kxs.product.biz.mapper.KxsBrandMapper;
import com.kxs.product.biz.mapper.KxsMachineDeductMapper;
import com.kxs.product.api.model.KxsMachineDeduct;
import com.kxs.product.biz.service.KxsMachineDeductService;
import com.pig4cloud.plugin.excel.vo.ErrorMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 机具循环过期已扣款记录表(KxsMachineDeduct)表服务实现类
 *
 * @author 系统
 * @since 2024-03-07 15:29:06
 */
@Service("kxsMachineDeductService")
@RequiredArgsConstructor
public class KxsMachineDeductServiceImpl extends ServiceImpl<KxsMachineDeductMapper, KxsMachineDeduct> implements KxsMachineDeductService {

    private final KxsBrandMapper kxsBrandMapper;

    @Override
    public R importData(List<MachineDeductExcelVo> machineDeductExcelVos, BindingResult bindingResult) {

        if (machineDeductExcelVos.isEmpty()) {
            return R.failed(ProductErrorTypeEnum.IMPORT_EMPTY.getDescription());
        }
        //查询所有品牌
        List<KxsBrand> brands = kxsBrandMapper.selectList(Wrappers.emptyWrapper());

        // 通用校验获取失败的数据
        List<ErrorMessage> errorMessageList = (List<ErrorMessage>) bindingResult.getTarget();
        // 执行数据插入操作
        for (MachineDeductExcelVo excel : machineDeductExcelVos) {
            Set<String> errorMsg = new HashSet<>();
            KxsMachineDeduct machineDeduct = new KxsMachineDeduct();
            BeanUtil.copyProperties(excel, machineDeduct);

            Long count = baseMapper.selectCount(Wrappers.<KxsMachineDeduct>lambdaQuery().eq(KxsMachineDeduct::getPosSn, excel.getPosSn()));
            if(count > 0){
                errorMsg.add(excel.getPosSn() + "已存在，请重新上传！");
            }
            //查询品牌
//            KxsBrand brand = brands.stream().filter(b -> b.getName().equals(excel.getBrandName())).findFirst().orElse(null);
//            // 数据不合法
//            if(brand == null){
//                errorMsg.add(ProductErrorTypeEnum.BRAND_EMPTY.getDescription());
//            }else{
//                machineDeduct.setBrandId(brand.getId());
//            }
            // 数据合法情况
            if (CollUtil.isEmpty(errorMsg)) {
                this.save(machineDeduct);
            }
            else {
                // 数据不合法
                assert errorMessageList != null;
                errorMessageList.add(new ErrorMessage(excel.getLineNum(), errorMsg));
            }
        }
        if (CollUtil.isNotEmpty(errorMessageList)) {
            return R.failed(JSON.toJSONString(errorMessageList));
        }
        return R.ok();
    }
}

