package com.zcloud.modules.invoicing.service.impl;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.zcloud.common.utils.PageUtils;
 import com.zcloud.datasource.annotation.DataSource;
 import com.zcloud.modules.custormerInfo.entity.BusInvoiceManagementEntity;
 import com.zcloud.modules.custormerInfo.service.BusInvoiceManagementService;
 import com.zcloud.modules.invoicing.dao.BusInvoicingDao;
 import com.zcloud.modules.invoicing.entity.BusInvoicingEntity;
 import com.zcloud.modules.invoicing.entity.BusInvoicingReturnEntity;
 import com.zcloud.modules.invoicing.service.BusInvoicingReturnService;
 import com.zcloud.modules.invoicing.service.BusInvoicingService;
 import org.apache.commons.beanutils.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Author lizenan
 * @Desc 开票管理表(BusInvoicing)表服务实现类
 * @Date 2024-04-03 15:15:38
 */
@Service("busInvoicingService")
public class BusInvoicingServiceImpl extends ServiceImpl<BusInvoicingDao, BusInvoicingEntity> implements BusInvoicingService {

    @Autowired
    private BusInvoiceManagementService busInvoiceManagementService;
    @Autowired
    private BusInvoicingReturnService busInvoicingReturnService;
    @Resource
    private BusInvoicingDao busInvoicingDao;

    @Override
    @DataSource("slave1")
	public BusInvoicingEntity queryByFlexibleParams(String paramName, Object paramValue) {
		QueryWrapper<BusInvoicingEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(paramName, paramValue); // 根据传入的参数名和参数值动态构建查询条件

		return baseMapper.selectOne(queryWrapper);
	}

	@Override
    @DataSource("slave1")
	public List<BusInvoicingEntity> queryListByFlexibleParams(String paramName, Object paramValue) {
		QueryWrapper<BusInvoicingEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(paramName, paramValue); // 根据传入的参数名和参数值动态构建查询条件

		return baseMapper.selectList(queryWrapper);
	}

    @Override
    @DataSource("slave1")
    public PageUtils listPage(Map<String, Object> params) {
        Page<Map<String,Object>> page = new Page<>(Integer.parseInt(params.get("curPage").toString()),Integer.parseInt(params.get("limit").toString()));
        //申请时间
        if(params.get("applyTimes")!=null){
            List<String> applyTimes = (List<String>) params.get("applyTimes");
            params.put("applyTimeStart",applyTimes.get(0));
            params.put("applyTimeEnd",applyTimes.get(1));
        }
        params.put("isDelete",0);
        IPage<BusInvoicingEntity> ipage = baseMapper.listPage(page,params);
        return new PageUtils(ipage);
    }


   @Override
	public void delete(String invoicingId,String userId){
		UpdateWrapper<BusInvoicingEntity> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("invoicing_id",invoicingId);

		BusInvoicingEntity busInvoicing = new BusInvoicingEntity();
		busInvoicing.setOperator(userId);
		busInvoicing.setIsDelete(1);

		this.update(busInvoicing, updateWrapper);
	}

    @Override
    @DataSource("slave1")
    public BusInvoicingEntity info(BusInvoicingEntity params) {
        BusInvoicingEntity busInvoicingEntity = busInvoicingDao.getInvoiceDetail(params);
        List<BusInvoicingReturnEntity> invoicingReturnList = busInvoicingReturnService.listByInvoicingId(params.getInvoicingId());
        busInvoicingEntity.setInvoicingReturnList(invoicingReturnList);
        return busInvoicingEntity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BusInvoicingEntity param) throws Exception {
        //先判断是否改了发票抬头
        if(param.getIsChange() == 1){
            BusInvoiceManagementEntity busInvoiceManagementEntity = new BusInvoiceManagementEntity();
            BeanUtils.copyProperties(busInvoiceManagementEntity,param);
            busInvoiceManagementEntity.setInvoiceName(param.getInvoiceIdStr());
            busInvoiceManagementService.save(busInvoiceManagementEntity);
            param.setInvoiceId(busInvoiceManagementEntity.getInvoiceId());
        }
        baseMapper.updateById(param);
    }
}
