package com.izouma.zhirongip.service.supply;

import cn.hutool.core.collection.CollUtil;
import com.izouma.zhirongip.domain.Information;
import com.izouma.zhirongip.domain.Setting;
import com.izouma.zhirongip.domain.supply.Copyright;
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.CopyrightRepo;
import com.izouma.zhirongip.utils.JpaUtils;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class CopyrightService {

    private final CopyrightRepo   copyrightRepo;
    private final SettingRepo     settingRepo;
    private final InformationRepo informationRepo;

    public Page<Copyright> all(PageQuery pageQuery) {
        Map<Long, String> settingMap = settingRepo.findAllByFlagIn(CollUtil.list(false, 3, 4, 6,24))
                .stream()
                .collect(Collectors.toMap(Setting::getId, Setting::getName));
        return copyrightRepo.findAll(JpaUtils.toSpecification(pageQuery, Copyright.class), JpaUtils.toPageRequest(pageQuery))
                .map(cd -> {
                    cd.setWorkName(settingMap.get(cd.getWorkType()));
                    if(cd.getTradingMethodId()!=null){
                        cd.setTradingMethod(settingMap.get(cd.getTradingMethodId()));
                    }
                    return cd;
                });
    }

    public Copyright get(Long id) {
        Copyright copyright = copyrightRepo.findById(id).orElseThrow(new BusinessException("无记录"));
        copyright.setWorkName(settingRepo.findById(copyright.getWorkType())
                .orElseThrow(new BusinessException("无记录"))
                .getName());
        if(copyright.getTradingMethodId()!=null){
            copyright.setTradingMethod(settingRepo.findById(copyright.getTradingMethodId())
                    .orElseThrow(new BusinessException("无记录"))
                    .getName());
        }
        return copyright;
    }

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

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

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

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

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