package org.ta.pddserver.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.ta.pddserver.entity.CollectOrderEntity;
import org.ta.pddserver.model.api.request.CollectMailDetailListRequest;
import org.ta.pddserver.model.api.request.CollectOrderListRequest;
import org.ta.pddserver.model.api.response.CollectMailDetailListResponse;
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}%' AND a.`status` LIKE '%${status}%' ${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}%' AND a.`status` LIKE '%${status}%' ${str} GROUP BY a.`uid` ORDER BY a.`created` DESC LIMIT #{offset}, #{size}; ")
    List<CollectOrderListResponse> getListByRequest(CollectOrderListRequest param);

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

}
