package com.scm.contract.service.purchase;

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.purchase.TagPurchaseDetailDao;
import com.scm.contract.model.apply.DictionaryTagType;
import com.scm.contract.model.purchase.TagPurchaseDetail;
import com.scm.contract.service.apply.DictionaryTagTypeService;
import com.scm.contract.tools.Tools;
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/6/21 8:23
 */
@Service
public class TagPurchaseDetailService {

    @Autowired
    private TagPurchaseDetailDao detailDao;

    @Autowired
    private DictionaryTagTypeService tagTypeService;

    public List<TagPurchaseDetail> getListByTagPurchaseId(String tagPurchaseId) {
        List<TagPurchaseDetail> details = detailDao.getListByTagPurchaseId(tagPurchaseId);
        setName(details);
        return details;
    }

    public List<TagPurchaseDetail> getListByTagPurchaseIds(List<String> tagPurchaseIds) {
        List<TagPurchaseDetail> details = detailDao.getListByTagPurchaseIds(tagPurchaseIds);
        setName(details);
        return details;
    }

    public void setName(List<TagPurchaseDetail> details) {
        if (CollectionUtils.isNotEmpty(details)) {
            Set<String> tagTypeIds = new HashSet<>();
            for (TagPurchaseDetail detail : details) {
                tagTypeIds.add(detail.getTagTypeId());
            }
            List<DictionaryTagType> tagTypes = tagTypeService.listByIds(new ArrayList<>(tagTypeIds));

            Map<String, DictionaryTagType> tagTypeMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(tagTypes)) {
                for (DictionaryTagType tagType : tagTypes) {
                    tagTypeMap.put(tagType.getId(), tagType);
                }
            }

            for (TagPurchaseDetail detail : details) {
                Long number = detail.getNumber();
                Double unitPrice = detail.getUnitPrice();
                if (!Objects.isNull(number) && !Objects.isNull(unitPrice)) {
                    detail.setAmount(Tools.digitHalfEven(number * unitPrice, 2));
                }
                String tagTypeId = detail.getTagTypeId();
                DictionaryTagType tagType = tagTypeMap.getOrDefault(tagTypeId, new DictionaryTagType());
                detail.setTagTypeName(tagType.getName());
            }
        }
    }

    @Transactional
    public synchronized Boolean deleteListByTagPurchaseId(String tagPurchaseId) {
        return detailDao.deleteListByTagPurchaseId(tagPurchaseId);
    }

    @Transactional
    public synchronized Boolean saveList(List<TagPurchaseDetail> details) {
        for (TagPurchaseDetail detail : details) {
            String id = detail.getId();
            if (StringUtils.isBlank(id)){
                detail.setId(IdWorker.getIdStr());
            }
        }
        return detailDao.saveList(details);
    }
}
