package org.ta.pddserver.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.ta.pdd.entity.CollectOrderEntity;
import org.ta.pddserver.model.api.request.CollectOrderListRequest;
import org.ta.pddserver.model.api.response.CollectOrderInfoResponse;
import org.ta.pddserver.model.api.response.CollectOrderListResponse;
import org.ta.pddserver.model.api.response.CollectOrderResponse;

import java.util.List;

@Mapper
public interface CollectOrderMapper extends BaseMapper<CollectOrderEntity> {

    @Select("SELECT `uid`,`orderCode`, `itemQuantityTotal`, `segmentCode`, `consoType`, `remarks` FROM `collect_order` WHERE `orderCode` = #{orderCode};")
    CollectOrderResponse findByOrderCode(String orderCode);

    @Update("UPDATE `collect_order` SET `status` = #{status} WHERE `uid` = #{uid};")
    int updateOrderStatusById(@Param("status")int status, @Param("uid")String uid);

    @Select("SELECT `uid`, `orderCode`, `itemQuantityTotal`, `segmentCode`, `consoType` FROM `collect_order` WHERE `orderCode` = #{orderCode} AND `status` = ${status}")
    List<CollectOrderListResponse> getList(@Param("orderCode") String orderCode, @Param("status") int status);

    @Select("SELECT `uid`, `orderCode`, `itemQuantityTotal`, `tradeOrderActualAmountTotal`,`segmentCode`, `consoType`, `deliveryType`, `created` FROM `collect_order` WHERE `orderCode` = #{orderCode};")
    CollectOrderInfoResponse getInfoByOrderCode(@Param("orderCode") String orderCode);

    @Select("SELECT COUNT(*) FROM (SELECT a.`uid` FROM `collect_order` a LEFT JOIN `collect_mail_detail` b ON a.`orderCode` = b.`orderCode` WHERE a.`orderCode` LIKE '%${orderCode}%' ${str} GROUP BY a.`uid`) t; ")
    Integer countUsable(CollectOrderListRequest param);

    @Select("SELECT a.`uid`, a.`orderCode`, a.`itemQuantityTotal`, GROUP_CONCAT(b.`mailNo` SEPARATOR ';') as mailNo, a.`created`, a.`num` FROM `collect_order` a LEFT JOIN `collect_mail_detail` b ON a.`orderCode` = b.`orderCode` WHERE a.`orderCode` LIKE '%${orderCode}%' ${str} GROUP BY a.`uid` ORDER BY a.`created` DESC LIMIT #{offset}, #{size}; ")
    List<CollectOrderListResponse> getListByRequest(CollectOrderListRequest param);

    @Select("SELECT a.`uid`, a.`orderCode`, a.`itemQuantityTotal`, a.`created`, a.`num` FROM `collect_order` a WHERE a.`orderCode` LIKE '%${orderCode}%' AND a.`num` = 0 ORDER BY a.`created` ASC ; ")
    List<CollectOrderListResponse> getUnprintListByRequest(CollectOrderListRequest param);

    @Select("SELECT * FROM `collect_order` WHERE `orderCode` = #{orderCode};")
    CollectOrderEntity getAllInfoByOrderCode(@Param("orderCode") String orderCode);

    @Update("UPDATE `collect_order` SET `num` = `num` + #{num} WHERE `orderCode` = #{orderCode};")
    int updatePrintNum(@Param("num") int num, @Param("orderCode") String orderCode);

    @Update("UPDATE `collect_order` SET `status` = #{status} WHERE `orderCode` = #{orderCode};")
    int updateStatusBuOrderCode(@Param("status") Integer status, @Param("orderCode") String orderCode);

    @Delete("DELETE FROM `collect_order` WHERE `orderCode` = #{orderCode};")
    Integer deleteByOrderCode(@Param("orderCode") String orderCode);
}
