package com.jeeplus.test.cw.workClientInfo.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.test.cw.workClientInfo.domain.CwWorkClientBase;
import com.jeeplus.test.cw.workClientInfo.domain.CwWorkClientBilling;
import com.jeeplus.test.cw.workClientInfo.mapper.CwWorkClientBillingMapper;
import com.jeeplus.test.cw.workClientInfo.service.dto.CwWorkClientBillingDTO;
import com.jeeplus.test.cw.workClientInfo.service.mapstruct.CwWorkClientBaseWrapper;
import com.jeeplus.test.cw.workClientInfo.service.mapstruct.CwWorkClientBillingWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class CwWorkClientBillingService extends ServiceImpl<CwWorkClientBillingMapper, CwWorkClientBilling> {

    @Resource
    private CwWorkClientBillingMapper cwWorkClientBillingMapper;

    /**
     * 根据客户id查询开票信息
     * @param cwWorkClientBillingDTO
     * @return
     */
    public List<CwWorkClientBillingDTO> getBillingListByClientId(CwWorkClientBillingDTO cwWorkClientBillingDTO) throws Exception {
        QueryWrapper<CwWorkClientBilling> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cw_wcbi.del_flag", "0");
        if (ObjectUtil.isNotEmpty(cwWorkClientBillingDTO) && StringUtils.isNotBlank(cwWorkClientBillingDTO.getClientId())) {
            queryWrapper.eq("cw_wcbi.client_id", cwWorkClientBillingDTO.getClientId());
            if (StringUtils.isNotBlank(cwWorkClientBillingDTO.getCompanyName())) { // 公司名称
                queryWrapper.like("cw_wcbi.company_name", cwWorkClientBillingDTO.getCompanyName());
            }
            if (StringUtils.isNotBlank(cwWorkClientBillingDTO.getTaxpayerIdentificationNo())) { // 纳税人识别号
                queryWrapper.like("cw_wcbi.taxpayer_identification_no", cwWorkClientBillingDTO.getTaxpayerIdentificationNo());
            }
        } else {
            return new ArrayList<>();
        }
        return cwWorkClientBillingMapper.getBillingListByClientId(queryWrapper);
    }
}
