package com.chushang.inspection.project.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.chushang.common.core.exception.utils.AssertUtil;
import com.chushang.common.mybatis.page.CommonParam;
import com.chushang.common.mybatis.utils.PageResult;
import com.chushang.common.mybatis.utils.WrapperUtils;
import com.chushang.inspection.project.dto.PageAuditDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chushang.inspection.project.mapper.WrkProjectContractMapper;
import com.chushang.inspection.project.po.WrkProjectContract;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;

/**
  * @auther: zhao
  * @date: 2024/6/15 10:51
  */
@Slf4j
@Service
public class WrkProjectContractService extends ServiceImpl<WrkProjectContractMapper, WrkProjectContract> implements IService<WrkProjectContract> {
    /**
     * 删除合同
     */
    @Transactional
    public void delete(Long contractId) {
        WrkProjectContract contract = getById(contractId);
        Integer auditStatus = contract.getAuditStatus();
        // 不为 0 时, 合同不能删除
        AssertUtil.invalidate(0 != auditStatus && 7 != auditStatus, "当前合同无法删除, 请撤销后删除");

        removeById(contractId);
    }

//    public PageResult pageList(Long projectId, CommonParam commonParam) {
//        WrkProjectContract contract = WrkProjectContract.builder().projectId(projectId).build();
//        return pageList(contract, commonParam);
//    }

    public PageResult pageList(WrkProjectContract contract, CommonParam commonParam) {
        LambdaQueryWrapper<WrkProjectContract> queryWrapper = WrapperUtils.builder(contract, commonParam);
        IPage<WrkProjectContract> page = this.page(
                new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(commonParam.getPage(), commonParam.getLimit()),
                queryWrapper
        );
        return new PageResult(page);
    }


    public WrkProjectContract queryListByProjectIds(Long projectId) {
        return baseMapper.queryListByProjectIds(projectId);
    }


    /**
     * 修改 合同的 审核状态
     */
    public void updateAuditStatus(Long contractId, Integer auditStatus) {
        updateById(WrkProjectContract.builder()
                .contractId(contractId)
                .auditStatus(auditStatus)
                .build());
    }
}
