package com.fjhx.sale.service.purchase.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.sale.entity.purchase.dto.EhsdPurchaseDto;
import com.fjhx.sale.entity.purchase.dto.EhsdPurchaseSelectDto;
import com.fjhx.sale.entity.purchase.po.EhsdPurchase;
import com.fjhx.sale.entity.purchase.vo.EhsdPurchaseVo;
import com.fjhx.sale.mapper.purchase.EhsdPurchaseMapper;
import com.fjhx.sale.service.purchase.EhsdPurchaseService;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.common.utils.wrapper.SqlField;
import org.springframework.stereotype.Service;


/**
 * <p>
 * ehsd采购表 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-05-17
 */
@Service
@DS(SourceConstant.SALE)
public class EhsdPurchaseServiceImpl extends ServiceImpl<EhsdPurchaseMapper, EhsdPurchase> implements EhsdPurchaseService {

    /**
     * 分页
     * @param dto
     * @return
     */
    @Override
    public Page<EhsdPurchaseVo> getPage(EhsdPurchaseSelectDto dto) {
        IWrapper<EhsdPurchase> wrapper = getWrapper();
        wrapper.orderByDesc("t1", EhsdPurchase::getCreateTime);
        if (ObjectUtils.isNotEmpty(dto.getPurchaseStatus())) {
            wrapper.eq("t1", EhsdPurchase::getStatus, dto.getPurchaseStatus());
        }
        if (ObjectUtils.isNotEmpty(dto.getPayStatus())) {
            wrapper.eq("t1", EhsdPurchase::getPayStatus, dto.getPayStatus());
        }
        if (ObjectUtils.isNotEmpty(dto.getKeyword())) {
            wrapper.keyword(dto.getKeyword(),new SqlField("t2.name"),new SqlField("t1.`code`"));
        }
        Page<EhsdPurchaseVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

    @Override
    public EhsdPurchaseVo detail(Long id) {
        EhsdPurchase EhsdPurchase = this.getById(id);
        EhsdPurchaseVo result = BeanUtil.toBean(EhsdPurchase, EhsdPurchaseVo.class);
        return result;
    }

    @Override
    public void add(EhsdPurchaseDto ehsdPurchaseDto) {
        this.save(ehsdPurchaseDto);
    }

    @Override
    public void edit(EhsdPurchaseDto ehsdPurchaseDto) {
        this.updateById(ehsdPurchaseDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

}
