package cn.iocoder.yudao.module.order.dal.mysql.targetLog;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQuery;
import cn.iocoder.yudao.framework.mybatis.core.mapper.AbstractMapper;
import cn.iocoder.yudao.module.order.dal.dataobject.targetLog.TargetLogDO;
import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.order.vo.targetLog.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 部门业绩日志 Mapper
 * @author 系统管理员
 */
@Mapper
public interface TargetLogMapper extends AbstractMapper<TargetLogDO> {
    @Override
    default PageResult<TargetLogDO> selectPage(PageVO page, Object object) {
        if (object instanceof TargetLogQueryVO) {
            TargetLogQueryVO vo = (TargetLogQueryVO)object;
            return selectPage(page, new LambdaQuery<TargetLogDO>()
                .betweenIfPresent(TargetLogDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                .eqIfPresent(TargetLogDO::getOrderId, vo.getOrderId())
                .eqIfPresent(TargetLogDO::getUserId, vo.getUserId())
                .eqIfPresent(TargetLogDO::getCompleteVolume, vo.getCompleteVolume())
                .betweenIfPresent(TargetLogDO::getAchievementTime, vo.getBeginAchievementTime(), vo.getEndAchievementTime())
                    .orderByDesc(TargetLogDO::getId));
        }

        return null;
    }

    @Override
    default List<TargetLogDO> selectList(Object object) {
        if (object instanceof TargetLogQueryVO) {
            TargetLogQueryVO vo = (TargetLogQueryVO)object;
            return selectList(new LambdaQuery<TargetLogDO>()
                .betweenIfPresent(TargetLogDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                .eqIfPresent(TargetLogDO::getOrderId, vo.getOrderId())
                .eqIfPresent(TargetLogDO::getUserId, vo.getUserId())
                .eqIfPresent(TargetLogDO::getCompleteVolume, vo.getCompleteVolume())
                .betweenIfPresent(TargetLogDO::getAchievementTime, vo.getBeginAchievementTime(), vo.getEndAchievementTime())
                    .orderByDesc(TargetLogDO::getId));
        }

        return null;
    }

    @Select({
            "<script>",
            "select t1.create_time from ecw_box_approval t1 where t1.shipment_id = (select t.shipment_id from ecw_box_preload_goods t where t.order_id=#{orderId} and t.deleted=0 limit 1) and t1.approval_type=2 and approval_status=2 limit 1",
            "</script>"
    })
    Date selectCabinetSealingTime(@Param("orderId") Long orderId);

    @Select({
            "<script>",
            "select a.ld_box_time from ecw_box_cabinet a LEFT JOIN ecw_box_preload_goods b on b.shipment_id=a.shipment_id and b.deleted=0 where b.order_id=#{orderId} limit 1",
            "</script>"
    })
    Date selectCabinetLoadTime(@Param("orderId") Long orderId);

    @Select({
            "<script>",
            "select offer_id as offerId, order_id as orderId,order_no as  orderNo  from ecw_offer offe   where deleted=0 and order_id=#{orderId} limit 1",
            "</script>"
    })
    List<TargetOfferBackVO> getTargetOfferBackByOrerId(Long orderId);


    @Select({
            "<script>",
            "select *  from ecw_target_log where deleted=0 and DATE_FORMAT(achievement_time,'%Y-%m')=#{sDate}",
            "</script>"
    })
    List<TargetLogDO> selectList2(String sDate);

    /*
    @Select({
            "<script>",
            "select *  from ecw_target_log where deleted=0 and (DATE_FORMAT(create_time,'%Y-%m')=#{sDate} OR DATE_FORMAT(achievement_time,'%Y-%m')=#{sDate})",
            "</script>"
    })
    List<TargetLogDO> selectList2(String sDate);

     */

}
