云
5 天以前 a82d847bbb39e2417d4c9773ebc4fc51fdb0b8fc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package cn.iocoder.yudao.module.crm.dal.mysql.quotation;
 
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import cn.iocoder.yudao.module.crm.controller.admin.quotation.vo.CrmSaleQuotationPageReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.quotation.CrmSaleQuotationDO;
import cn.iocoder.yudao.module.crm.enums.common.CrmBizTypeEnum;
import cn.iocoder.yudao.module.crm.enums.common.CrmSceneTypeEnum;
import cn.iocoder.yudao.module.crm.enums.quotation.CrmSaleQuotationStatusEnum;
import cn.iocoder.yudao.module.crm.util.CrmPermissionUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
 
import java.util.Collection;
import java.util.List;
 
/**
 * CRM 销售报价单 Mapper
 *
 * @author 超级管理员
 */
@Mapper
public interface CrmSaleQuotationMapper extends BaseMapperX<CrmSaleQuotationDO> {
 
    default CrmSaleQuotationDO selectByNo(String no) {
        return selectOne(CrmSaleQuotationDO::getNo, no);
    }
 
    /**
     * 按报价单号查询记录(不过滤逻辑删除,含已删除行)
     * 用于自定义单号的唯一性预校验:uk_no 唯一索引覆盖已删除行,需一起判断才能给出可读提示
     */
    @Select("SELECT * FROM crm_sale_quotation WHERE no = #{no} LIMIT 1")
    CrmSaleQuotationDO selectAnyByNo(String no);
 
    default PageResult<CrmSaleQuotationDO> selectPage(CrmSaleQuotationPageReqVO pageReqVO, Long userId) {
        return selectPage(pageReqVO, userId, null);
    }
 
    /**
     * 分页查询(带客户权限过滤)
     *
     * @param pageReqVO 查询条件
     * @param userId 用户ID
     * @param permittedCustomerIds 有权限的客户ID列表(null表示不限制)
     */
    default PageResult<CrmSaleQuotationDO> selectPage(CrmSaleQuotationPageReqVO pageReqVO, Long userId, List<Long> permittedCustomerIds) {
        MPJLambdaWrapperX<CrmSaleQuotationDO> query = new MPJLambdaWrapperX<>();
        // 拼接数据权限的查询条件
        CrmPermissionUtils.appendPermissionCondition(query, CrmBizTypeEnum.CRM_SALE_QUOTATION.getType(),
                CrmSaleQuotationDO::getId, userId, pageReqVO.getSceneType());
        // 拼接自身的查询条件
        query.selectAll(CrmSaleQuotationDO.class)
                .likeIfPresent(CrmSaleQuotationDO::getNo, pageReqVO.getNo())
                .likeIfPresent(CrmSaleQuotationDO::getName, pageReqVO.getName())
                .eqIfPresent(CrmSaleQuotationDO::getCustomerId, pageReqVO.getCustomerId())
                .eqIfPresent(CrmSaleQuotationDO::getBusinessId, pageReqVO.getBusinessId())
                .eqIfPresent(CrmSaleQuotationDO::getStatus, pageReqVO.getStatus())
                .likeIfPresent(CrmSaleQuotationDO::getBidBondNo, pageReqVO.getBidBondNo())
                .orderByDesc(CrmSaleQuotationDO::getId);
 
        // 客户权限过滤
        if (permittedCustomerIds != null) {
            if (CollUtil.isEmpty(permittedCustomerIds)) {
                return PageResult.empty();
            }
            query.in(CrmSaleQuotationDO::getCustomerId, permittedCustomerIds);
        }
 
        return selectJoinPage(pageReqVO, CrmSaleQuotationDO.class, query);
    }
 
    default PageResult<CrmSaleQuotationDO> selectPageByCustomerId(CrmSaleQuotationPageReqVO pageReqVO) {
        return selectPage(pageReqVO, new LambdaQueryWrapperX<CrmSaleQuotationDO>()
                .eq(CrmSaleQuotationDO::getCustomerId, pageReqVO.getCustomerId())
                .likeIfPresent(CrmSaleQuotationDO::getNo, pageReqVO.getNo())
                .likeIfPresent(CrmSaleQuotationDO::getName, pageReqVO.getName())
                .orderByDesc(CrmSaleQuotationDO::getId));
    }
 
    default PageResult<CrmSaleQuotationDO> selectPageByBusinessId(CrmSaleQuotationPageReqVO pageReqVO) {
        return selectPage(pageReqVO, new LambdaQueryWrapperX<CrmSaleQuotationDO>()
                .eq(CrmSaleQuotationDO::getBusinessId, pageReqVO.getBusinessId())
                .likeIfPresent(CrmSaleQuotationDO::getNo, pageReqVO.getNo())
                .likeIfPresent(CrmSaleQuotationDO::getName, pageReqVO.getName())
                .orderByDesc(CrmSaleQuotationDO::getId));
    }
 
    default List<CrmSaleQuotationDO> selectListByCustomerId(Long customerId) {
        return selectList(CrmSaleQuotationDO::getCustomerId, customerId);
    }
 
    default List<CrmSaleQuotationDO> selectListByCustomerIdAndStatuses(Long customerId,
                                                                      Collection<Integer> statuses) {
        return selectList(new LambdaQueryWrapperX<CrmSaleQuotationDO>()
                .eq(CrmSaleQuotationDO::getCustomerId, customerId)
                .in(CrmSaleQuotationDO::getStatus, statuses)
                .orderByDesc(CrmSaleQuotationDO::getId));
    }
 
    default List<CrmSaleQuotationDO> selectListByBusinessId(Long businessId) {
        return selectList(CrmSaleQuotationDO::getBusinessId, businessId);
    }
 
    default Long selectCountByBusinessId(Long businessId) {
        return selectCount(CrmSaleQuotationDO::getBusinessId, businessId);
    }
 
    default Long selectCountByCustomerId(Long customerId) {
        return selectCount(CrmSaleQuotationDO::getCustomerId, customerId);
    }
 
    /**
     * 清除报价单的合同关联(删除合同时调用)
     *
     * @param quotationId 报价单编号
     */
    default void clearContractReference(Long quotationId) {
        update(new LambdaUpdateWrapper<CrmSaleQuotationDO>()
                .eq(CrmSaleQuotationDO::getId, quotationId)
                .set(CrmSaleQuotationDO::getContractId, null)
                .set(CrmSaleQuotationDO::getStatus,0)
                .set(CrmSaleQuotationDO::getContractNo, null));
    }
 
    /**
     * 绑定投标;bidBondId 为 null 时表示解绑
     */
    default void bindBidBond(Long quotationId, Long bidBondId, String bidBondNo) {
        update(new LambdaUpdateWrapper<CrmSaleQuotationDO>()
                .eq(CrmSaleQuotationDO::getId, quotationId)
                .set(CrmSaleQuotationDO::getBidBondId, bidBondId)
                .set(CrmSaleQuotationDO::getBidBondNo, bidBondNo));
    }
 
    /**
     * 重新报价:重置为草稿并清空流程实例
     */
    default void resetToDraft(Long quotationId) {
        update(new LambdaUpdateWrapper<CrmSaleQuotationDO>()
                .eq(CrmSaleQuotationDO::getId, quotationId)
                .set(CrmSaleQuotationDO::getStatus, CrmSaleQuotationStatusEnum.DRAFT.getStatus())
                .set(CrmSaleQuotationDO::getProcessInstanceId, null));
    }
 
}