package com.jwipc.tender.logic.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.aggregation.ModelAggregationService;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.client.UserPurchaseGroupClient;
import com.jwipc.tender.constant.BidStatus;
import com.jwipc.tender.constant.DepositStatus;
import com.jwipc.tender.convert.SecurityDepositAccountConvert;
import com.jwipc.tender.convert.TenderSecurityDepositConvert;
import com.jwipc.tender.dto.SecurityDepositAccountBaseDTO;
import com.jwipc.tender.dto.TenderDepositApprovelListDTO;
import com.jwipc.tender.entity.SecurityDepositAccount;
import com.jwipc.tender.entity.TenderBid;
import com.jwipc.tender.entity.TenderSecurityDeposit;
import com.jwipc.tender.logic.SecurityDepositAccountLogic;
import com.jwipc.tender.model.po.TenderBidListPO;
import com.jwipc.tender.model.query.BidQuery;
import com.jwipc.tender.service.ISecurityDepositAccountService;
import com.jwipc.tender.service.ITenderBidService;
import com.jwipc.tender.service.ITenderSecurityDepositService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * <p> </p>
 *
 * @author BOGON
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@RequiredArgsConstructor
public class SecurityDepositAccountLogicImpl implements SecurityDepositAccountLogic {
    private final ISecurityDepositAccountService securityDepositAccountService;
    private final SecurityDepositAccountConvert securityDepositAccountConvert;
    private final ITenderSecurityDepositService tenderSecurityDepositService;
    private final TenderSecurityDepositConvert tenderSecurityDepositConvert;
    private final ModelAggregationService modelAggregationService;
    private final ITenderBidService tenderBidService;
    private final UserPurchaseGroupClient userPurchaseGroupClient;

    @Override
    public SecurityDepositAccountBaseDTO getDetail(Long id) {
        final SecurityDepositAccount depositAccount = securityDepositAccountService.getById(id);
        return securityDepositAccountConvert.entity2Dto(depositAccount);
    }

    @Override
    public List<SecurityDepositAccountBaseDTO> getListAll() {
        final List<SecurityDepositAccount> list = securityDepositAccountService.list();
        return securityDepositAccountConvert.entities2Dtos(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(SecurityDepositAccountBaseDTO securityDepositAccountBaseDTO) {
        SecurityDepositAccount securityDepositAccount = securityDepositAccountConvert.dto2Entity(
                securityDepositAccountBaseDTO);
        securityDepositAccountService.saveOrUpdate(securityDepositAccount);
    }

    @Override
    public PageView getApprovelList(BidQuery bidQuery) {
        Page<TenderBidListPO> page = PageUtil.toPage(bidQuery);
        final Set<String> userPurchaseGroupCode = userPurchaseGroupClient.getUserPurchaseGroupCode(
                LoginUtil.getUserId());
        bidQuery.setUserPurchaseGroupCodes(userPurchaseGroupCode);
        List<TenderBidListPO> tenderBidListPOS = tenderSecurityDepositService.findApprovelList(page, bidQuery);
        List<TenderDepositApprovelListDTO> list = tenderSecurityDepositConvert.pos2Dtos(tenderBidListPOS);
        modelAggregationService.processRelationAggregation(list);
        return PageUtil.getPV(list, page.getTotal());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDepositStatus(String ids, Integer status,String reason) {
        //如果
        final List<String> idList = StrUtil.split(ids, StrUtil.COMMA);
        if (status == DepositStatus.APPROVED.getValue()) {
            //需要把投标状态改成待报价
            //驳回需要改状态
            final LambdaUpdateWrapper<TenderSecurityDeposit> updateWrapper =
                    new LambdaUpdateWrapper<TenderSecurityDeposit>().in(
                                    TenderSecurityDeposit::getId, idList)
                            .set(TenderSecurityDeposit::getStatus, status)
                            .set(TenderSecurityDeposit::getReason,reason);
            tenderSecurityDepositService.update(updateWrapper);
            final List<TenderSecurityDeposit> tenderSecurityDeposits = tenderSecurityDepositService.listByIds(idList);
            final Set<Long> bidIds = tenderSecurityDeposits.stream().map(TenderSecurityDeposit::getBidId)
                    .collect(Collectors.toSet());
            final LambdaUpdateWrapper<TenderBid> updateWrapper1 = new LambdaUpdateWrapper<TenderBid>().in(
                            TenderBid::getId, bidIds)
                    .set(TenderBid::getStatus, BidStatus.BIDDING.getStatus());
            tenderBidService.update(updateWrapper1);
        } else {

            //驳回需要改状态
            final LambdaUpdateWrapper<TenderSecurityDeposit> updateWrapper =
                    new LambdaUpdateWrapper<TenderSecurityDeposit>().in(
                                    TenderSecurityDeposit::getId, idList)
                            .set(TenderSecurityDeposit::getStatus, status)
                            .set(TenderSecurityDeposit::getReason,reason);

            tenderSecurityDepositService.update(updateWrapper);
        }
    }

    @Override
    public void updateMarginStatus(String ids, Integer status) {
        final List<String> idList = StrUtil.split(ids, StrUtil.COMMA);
        final LambdaUpdateWrapper<TenderSecurityDeposit> updateWrapper =
                new LambdaUpdateWrapper<TenderSecurityDeposit>().in(
                                TenderSecurityDeposit::getId, idList)
                        .set(TenderSecurityDeposit::getMarginStatus, status);
        tenderSecurityDepositService.update(updateWrapper);
    }
}