package com.scm.contract.service.cancel;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.scm.contract.dao.cancel.TagCancelDao;
import com.scm.contract.model.apply.DictionaryTagType;
import com.scm.contract.model.cancel.TagCancel;
import com.scm.contract.model.management.TagData;
import com.scm.contract.service.apply.DictionaryTagTypeService;
import com.scm.contract.service.management.TagDataService;
import com.scm.contract.tools.Tools;
import com.set.direwolf.core.entity.User;
import com.set.direwolf.core.mybatis.CrudService;
import com.set.direwolf.core.mybatis.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author zuo
 * @date 2024/7/25 11:59
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class TagCancelService extends CrudService<TagCancelDao, TagCancel, String> {

    @Autowired
    private DictionaryTagTypeService tagTypeService;

    @Autowired
    private TagDataService tagDataService;

    public void setName(List<TagCancel> list) {
        Set<String> tagTypeIds = new HashSet<>();
        for (TagCancel tagCancel : list) {
            String tagTypeId = tagCancel.getTagTypeId();
            tagTypeIds.add(tagTypeId);
        }
        Map<String, DictionaryTagType> tagTypeMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(tagTypeIds)) {
            List<DictionaryTagType> tagTypes = tagTypeService.listByIds(new ArrayList<>(tagTypeIds));
            if (CollectionUtils.isNotEmpty(tagTypes)) {
                for (DictionaryTagType tagType : tagTypes) {
                    tagTypeMap.put(tagType.getId(), tagType);
                }
            }
        }

        for (TagCancel tagCancel : list) {
            String tagTypeId = tagCancel.getTagTypeId();
            if (StringUtils.isNotBlank(tagTypeId)) {
                tagCancel.setTagTypeName(tagTypeMap.getOrDefault(tagTypeId, new DictionaryTagType()).getName());
            }
        }
    }

    /**
     * 分页查询
     *
     * @param page
     * @param entity
     * @return
     */
    @Override
    public Page<TagCancel> findPage(Page<TagCancel> page, TagCancel entity) {
        entity.setPage(page);
        page.setList(this.dao.findList(entity));
        List<TagCancel> list = page.getList();
        setName(list);
        return page;
    }

    /**
     * 手动作废标签
     * @param tagCancel
     */
    @Transactional
    public synchronized void saveTagCancel(TagCancel tagCancel) {
        String tagTypeId = tagCancel.getTagTypeId();
        Long number = tagCancel.getNumber();
        if (StringUtils.isBlank(tagTypeId)) {
            throw new RuntimeException("作废标签类型不能为空");
        }
        if (Objects.isNull(number)) {
            throw new RuntimeException("作废标签数量不能为空");
        }
        //获取当前可使用库存,如果作废数量超过当前可使用库存数量，提示作废失败
        Map<String, Long> surplusNumberMap = tagDataService.getSurplusNumber();
        Long surplusNumber = surplusNumberMap.getOrDefault(tagTypeId, 0L);
        if ((surplusNumber - number) < 0) {
            throw new RuntimeException("当前标签剩余库存数量为『" + surplusNumber + "』，不足以扣减提交的作废数量");
        }
        String idStr = IdWorker.getIdStr();
        tagCancel.setId(idStr);
        User user = Tools.getUser();
        tagCancel.setOperatorId(user.getId());
        tagCancel.setOperatorName(user.getNickName());
        tagCancel.setStatus("0");
        this.save(tagCancel);
    }

    /**
     * 保存作废标签，并更新库存
     * @param tagCancel 已整理好完整的数据
     */
    @Transactional
    public synchronized void save(TagCancel tagCancel) {
        this.dao.save(tagCancel);
        List<TagData> tagDataList = new ArrayList<>();
        TagData tagData = new TagData();
        tagData.setDataType("3");
        tagData.setTagTypeId(tagCancel.getTagTypeId());
        tagData.setNumber(tagCancel.getNumber());
        tagData.setStatus(tagCancel.getStatus());
        tagDataList.add(tagData);
        tagDataService.saveOrUpdateBash(null, null, tagDataList, true);
    }

    @Transactional
    public synchronized void updateNumber(TagCancel tagCancel) {
        this.dao.updateNumber(tagCancel);
    }

    @Transactional
    public synchronized void deleteByReprintCodeId(String reprintCodeId) {
        this.dao.deleteByReprintCodeId(reprintCodeId);
    }
}
