package com.zfire.mall.manager.logic.manage.product.price;

import com.zfire.mall.manager.enums.StatusEnum;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.plus.entity.PriceSaleTypeRela;
import com.zfire.mall.manager.plus.entity.PriceType;
import com.zfire.mall.manager.plus.entity.PriceTypeBak;
import com.zfire.mall.manager.plus.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Slf4j
public class PriceTypeLogic {

    @Resource
    PriceTypeService priceTypeService;
    @Resource
    PriceTypeBakService priceTypeBakService;
    @Resource
    PriceSaleTypeRelaService priceSaleTypeRelaService;


    public PriceType detail(String priceTypeId) {
        return priceTypeService.lambdaQuery()
                .eq(PriceType::getPriceTypeId, priceTypeId)
                .one();
    }

    public Object add(PriceType priceType) {
        if (StringUtils.isBlank(priceType.getPriceTypeCode())) {
            throw new RemoteServiceException("价格类型编号不能为空");
        }
        if (StringUtils.isBlank(priceType.getPriceTypeName())) {
            throw new RemoteServiceException("价格类型不能为空");
        }
        Integer count = priceTypeService.lambdaQuery()
                .eq(PriceType::getPriceTypeCode, priceType.getPriceTypeCode())
                .or()
                .eq(PriceType::getPriceTypeName,priceType.getPriceTypeName())
                .count();
        if (count > 0) {
            throw new RemoteServiceException("价格类型编号或名称已存在");
        }

        return priceType.insert();
    }


    public void edit(PriceType priceType) {
        PriceType byId = priceTypeService.getById(priceType.getPriceTypeId());
        Integer count = priceTypeService.lambdaQuery()
                .ne(PriceType::getPriceTypeId, priceType.getPriceTypeId())
                .eq(PriceType::getPriceTypeName,priceType.getPriceTypeName())
                .count();
        if (count > 0) {
            throw new RemoteServiceException("价格类型名称已存在");
        }
        byId.setPriceTypeName(priceType.getPriceTypeName())
                .setStatus(priceType.getStatus())
                .updateById();
    }

    public void updateStatus(String priceTypeId, StatusEnum status) {
        priceTypeService.lambdaUpdate()
                .set(PriceType::getStatus, status.getKey())
                .eq(PriceType::getPriceTypeId, priceTypeId)
                .update();
    }

    public void del(List<String> ids) {
        List<PriceType> priceTypes = priceTypeService.listByIds(ids);
        List<PriceTypeBak> bakList = new ArrayList<>();
        List<PriceSaleTypeRela> typeRelas = priceSaleTypeRelaService.lambdaQuery()
                .in(PriceSaleTypeRela::getPriceTypeId, ids)
                .list();
        Map<String, PriceSaleTypeRela> relaMap = typeRelas.stream()
                .collect(Collectors.toMap(PriceSaleTypeRela::getPriceTypeId, Function.identity(), (key1, key2) -> key1));

        for (PriceType priceType : priceTypes) {
            PriceSaleTypeRela rela = relaMap.get(priceType.getPriceTypeId());
            if (Objects.nonNull(rela)) {
                throw new RemoteServiceException(priceType.getPriceTypeCode() + "价格编号已关联，请先更改价格销售类型关系");
            }
            PriceTypeBak priceTypeBak = new PriceTypeBak();
            BeanUtils.copyProperties(priceType, priceTypeBak);
            bakList.add(priceTypeBak);
        }

        priceTypeService.removeByIds(priceTypes.stream().map(PriceType::getPriceTypeId).collect(Collectors.toList()));
        priceTypeBakService.saveBatch(bakList);
    }
}
