package com.fjhx.supplier.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.attachment.service.StockAttachmentService;
import com.fjhx.entity.attachment.StockAttachment;
import com.fjhx.entity.attachment.enums.AttachmentTypeEnum;
import com.fjhx.entity.supplier.SupplierProblem;
import com.fjhx.params.supplier.SupplierProblemVo;
import com.fjhx.supplier.mapper.SupplierProblemMapper;
import com.fjhx.supplier.service.SupplierProblemService;
import com.fjhx.utils.WrapperUtil;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 供应商面料问题反馈表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-07-12
 */
@Service
public class SupplierProblemServiceImpl extends ServiceImpl<SupplierProblemMapper, SupplierProblem> implements SupplierProblemService {

    @Autowired
    private StockAttachmentService stockAttachmentService;


    @Override
    public Page<SupplierProblem> getPage(Map<String, String> condition) {

        QueryWrapper<SupplierProblem> wrapper = Wrappers.query();

        WrapperUtil.init(condition, wrapper)
                .eqTenantId()
                .createTimeDesc();

        Page<SupplierProblem> page = page(condition, wrapper);
        return page;
    }

    /**
     * 保存
     *
     * @param supplierProblemVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SupplierProblemVo supplierProblemVo) {
        if (StringUtil.isEmpty(supplierProblemVo.getProblemType())) {
            throw new ServiceException("参数异常");
        }
        //保存数据
        supplierProblemVo.setCreatedTime(new Date());
        supplierProblemVo.setProblemTypeCount(String.valueOf(Arrays.asList(supplierProblemVo.getProblemType().split(",")).size()));
        save(supplierProblemVo);
        List<StockAttachment> attr = supplierProblemVo.getAttr();
        if (CollectionUtil.isNotEmpty(attr)) {
            for (StockAttachment s : attr) {
                s.setBusiId(supplierProblemVo.getId());
                s.setBusiType(AttachmentTypeEnum.TYPE_ONE.getKey());
                s.setCreatedTime(new Date());
            }
        }
        stockAttachmentService.saveBatch(attr);
    }

    /**
     * 修改
     *
     * @param supplierProblemVo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SupplierProblemVo supplierProblemVo) {
        if (StringUtil.isEmpty(supplierProblemVo.getId())) {
            throw new ServiceException("参数异常");
        }
        supplierProblemVo.setUpdatedTime(new Date());
        updateById(supplierProblemVo);
        List<StockAttachment> attr = supplierProblemVo.getAttr();
        if (CollectionUtil.isNotEmpty(attr)) {
            //清空文件重新添加
            stockAttachmentService.remove(Wrappers.<StockAttachment>query().lambda().eq(StockAttachment::getBusiId, supplierProblemVo.getId()));
            for (StockAttachment s : attr) {
                s.setBusiId(supplierProblemVo.getId());
                s.setBusiType(AttachmentTypeEnum.TYPE_ONE.getKey());
            }
        }
        stockAttachmentService.saveBatch(attr);
    }

    /**
     * 删除
     *
     * @param supplierProblemVo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(SupplierProblemVo supplierProblemVo) {
        removeById(supplierProblemVo.getId());
        stockAttachmentService.remove(Wrappers.<StockAttachment>query().lambda().eq(StockAttachment::getBusiId, supplierProblemVo.getId()));
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public SupplierProblem detail(String id) {
        SupplierProblem vo = getById(id);
        if (ObjectUtil.isNotEmpty(vo)) {
            vo.setAttr(stockAttachmentService.getListByBusi(id));
        }
        return vo;
    }

    /**
     * 面料问题反馈
     *
     * @param supplierId
     * @return
     */
    @Override
    public Map<String, Integer> statisticsProblem(String supplierId) {
        String count = baseMapper.getGroupConcatType(supplierId);
        Map<String, Integer> map = new HashMap<>();
        if (ObjectUtil.isNotEmpty(count)) {
            String[] list = count.split(",");
            for (String string : list) {
                if (map.containsKey(string)) {
                    map.put(string, map.get(string) + 1);
                } else {
                    map.put(string, 1);
                }
            }
        }
        return map;
    }

    /**
     * 面料问题反馈条数
     *
     * @param supplierId
     * @return
     */
    @Override
    public Long statisticsProblemCount(String supplierId) {
        return baseMapper.selectCount(Wrappers.<SupplierProblem>query().lambda().eq(SupplierProblem::getSupplierId, supplierId));
    }
}
