package com.jwipc.contract.logic.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jwipc.constant.DelFlag;
import com.jwipc.contract.convert.ContractReturnOrderDetailsConvert;
import com.jwipc.contract.dto.ContractReturnOrderDetailDTO;
import com.jwipc.contract.entity.ContractReturnOrder;
import com.jwipc.contract.entity.ContractReturnOrderDetail;
import com.jwipc.contract.logic.ContractReturnOrderDetailLogic;
import com.jwipc.contract.model.po.ContractReturnOrderDetailPO;
import com.jwipc.contract.model.query.ContractReturnOrderDetailsListQuery;
import com.jwipc.contract.service.IContractReturnOrderDetailService;
import com.jwipc.contract.service.IContractReturnOrderService;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author qizhicai
 * @version JDK 1.8
 * @date 2025/12/16
 * @description TODO
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ContractReturnOrderDetailLogicImpl implements ContractReturnOrderDetailLogic {
    private final ContractReturnOrderDetailsConvert contractReturnOrderDetailsConvert;
    private final IContractReturnOrderDetailService contractReturnOrderDetailService;
    private final IContractReturnOrderService contractReturnOrderService;
    @Override
    public PageView getListReturnDetail(Long id) {
        ContractReturnOrderDetailsListQuery query = new ContractReturnOrderDetailsListQuery();
        query.setReturnOrderId(id);
        List<ContractReturnOrderDetailPO> po = contractReturnOrderDetailService.findList(query);
        List<ContractReturnOrderDetailDTO> dtos =  contractReturnOrderDetailsConvert.pos2Dtos(po);
        return PageUtil.getPV(dtos, dtos.size());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void remove(String ids) {
        final List<String> idList = StrUtil.split(ids, StrUtil.COMMA);
        if (CollectionUtils.isNotEmpty(idList)) {
            contractReturnOrderDetailService.update(
                    new LambdaUpdateWrapper<ContractReturnOrderDetail>()
                            .set(ContractReturnOrderDetail::getDeleteFlag, DelFlag.DELETE.getStatus())
                            .set(ContractReturnOrderDetail::getUpdateId,LoginUtil.getUserId())
                            .set(ContractReturnOrderDetail::getUpdateTime, LocalDateTime.now())
                            .in(ContractReturnOrderDetail::getId, idList)
            );
            ContractReturnOrderDetail detail = contractReturnOrderDetailService.getById(idList.get(0));
            long count = contractReturnOrderDetailService.count(new LambdaQueryWrapper<ContractReturnOrderDetail>().eq(ContractReturnOrderDetail::getReturnOrderId,detail.getReturnOrderId()).eq(ContractReturnOrderDetail::getDeleteFlag,DelFlag.NORMAL.getStatus()));
            if (count == 0L) {
                contractReturnOrderService.update(new LambdaUpdateWrapper<ContractReturnOrder>().set(ContractReturnOrder::getDeleteFlag,DelFlag.DELETE.getStatus()).set(ContractReturnOrder:: getUpdateId,LoginUtil.getUserId())
                        .set(ContractReturnOrder:: getUpdateTime,LocalDateTime.now())
                        .eq(ContractReturnOrder::getId, detail.getReturnOrderId()));
            }
        }
        //不需要推送共享，单据只有新建的章台可以编辑删除
    }
}
