package com.izouma.zhirongip.service.supply;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.izouma.zhirongip.converter.LongArrayConverter;
import com.izouma.zhirongip.domain.Information;
import com.izouma.zhirongip.domain.Setting;
import com.izouma.zhirongip.domain.supply.TechnologyProduct;
import com.izouma.zhirongip.dto.PageQuery;
import com.izouma.zhirongip.enums.ApplyStatus;
import com.izouma.zhirongip.exception.BusinessException;
import com.izouma.zhirongip.repo.InformationRepo;
import com.izouma.zhirongip.repo.SettingRepo;
import com.izouma.zhirongip.repo.supply.TechnologyProductRepo;
import com.izouma.zhirongip.utils.JpaUtils;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class TechnologyProductService {

    private final TechnologyProductRepo technologyProductRepo;
    private final SettingRepo           settingRepo;
    private final InformationRepo       informationRepo;

    public Page<TechnologyProduct> all1(PageQuery pageQuery) {
        return technologyProductRepo.findAll(JpaUtils.toSpecification(pageQuery, TechnologyProduct.class), JpaUtils.toPageRequest(pageQuery));
    }

    public Page<TechnologyProduct> all(PageQuery pageQuery, String money) {
        Map<Long, String> settingMap = settingRepo.findAllByFlagIn(CollUtil.list(false, 1,29,30))
                .stream()
                .collect(Collectors.toMap(Setting::getId, Setting::getName));
        return technologyProductRepo.findAll(((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> and = JpaUtils.toPredicates(pageQuery, TechnologyProduct.class, root, criteriaQuery, criteriaBuilder);
                    if (StrUtil.isNotBlank(money)) {
                        LongArrayConverter lc = new LongArrayConverter();
                        List<Long> money1 = lc.convertToEntityAttribute(money);
                        if (money1.size() > 1) {
                            and.add(criteriaBuilder.between(root.get("expectedPrice"), money1.get(0), money1.get(1)));
                        } else {
                            and.add(criteriaBuilder.greaterThanOrEqualTo(root.get("expectedPrice"), money1.get(0)));
                        }
                    }
                    return criteriaBuilder.and(and.toArray(new Predicate[0]));
                }), JpaUtils.toPageRequest(pageQuery))
                .map(tp -> {
                    tp.setFieldName(settingMap.get(tp.getField()));
                    if(tp.getTradingMethodId()!=null){
                        tp.setTradingMethod(settingMap.get(tp.getTradingMethodId()));
                    }
                    return tp;
                });
    }

    public void audit(Long id, boolean pass, String remark) {
        TechnologyProduct record = technologyProductRepo.findById(id).orElseThrow(new BusinessException("无记录"));

        Information information = Information.builder()
                .receiveUserId(record.getUserId())
                .isRead(false)
                .build();

        if (pass) {
            record.setStatus(ApplyStatus.PASS);
            technologyProductRepo.save(record);

            information.setContent("您发布的供给侧："+record.getName()+"已通过审核！");
            informationRepo.save(information);
            return;
        }
        record.setStatus(ApplyStatus.DENY);
        technologyProductRepo.save(record);

        information.setContent("您发布的的供给侧："+record.getName()+"未通过审核，原因为：" + remark);
        informationRepo.save(information);
    }
}
