package com.hfkj.dao;

import com.hfkj.entity.BsAgentMer;
import com.hfkj.model.AgentMerAllot;
import com.hfkj.openapi.model.response.ResponseQueryGasInfoModel;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * mapper扩展类
 */
public interface BsAgentMerMapperExt {

    @Insert(value = {
            "<script>" +
            " insert into bs_agent_mer (agent_id, agent_name, ",
            " agent_staff_id, agent_staff_name, ",
            " mer_source_type, province_code, ",
            " province_name, mer_id, ",
            " mer_no, mer_name, ",
            " qr_code_img_url, `status`, ",
            " create_time, update_time, ",
            " ext_1, ext_2, ext_3)",
            " values" +
            " <foreach collection=\"dataList\" item=\"item\" index=\"idx\" open=\"(\" separator=\"),(\" close=\")\">",
            " #{item.agentId,jdbcType=BIGINT}, #{item.agentName,jdbcType=VARCHAR}, ",
            " #{item.agentStaffId,jdbcType=BIGINT}, #{item.agentStaffName,jdbcType=VARCHAR}, ",
            " #{item.merSourceType,jdbcType=INTEGER}, #{item.provinceCode,jdbcType=BIGINT}, ",
            " #{item.provinceName,jdbcType=VARCHAR}, #{item.merId,jdbcType=BIGINT}, ",
            " #{item.merNo,jdbcType=VARCHAR}, #{item.merName,jdbcType=VARCHAR}, ",
            " #{item.qrCodeImgUrl,jdbcType=VARCHAR}, #{item.status,jdbcType=INTEGER}, ",
            " #{item.createTime,jdbcType=TIMESTAMP}, #{item.updateTime,jdbcType=TIMESTAMP}, ",
            " #{item.ext1,jdbcType=VARCHAR}, #{item.ext2,jdbcType=VARCHAR}, #{item.ext3,jdbcType=VARCHAR}",
            " </foreach>",
            "</script>"
    })
    void addPatch(@Param(value = "dataList") List<BsAgentMer> dataList);

    @Select({" <script> " +
            " select " +
            " a.agentMerId," +
            " a.merSourceType," +
            " a.provinceCode," +
            " a.provinceName," +
            " a.merNo," +
            " a.merName," +
            " a.merAddress," +
            " a.agentName," +
            " a.status," +
            " a.createTime," +
            " GROUP_CONCAT(a.agentStaffName) agentStaff" +
            "  FROM " +
            " (SELECT" +
            " a.id merId," +
            " a.source_type merSourceType," +
            " a.province_code provinceCode," +
            " a.province_name provinceName," +
            " a.mer_no merNo," +
            " a.mer_name merName," +
            " a.address merAddress," +
            " a.status status," +
            " b.id agentMerId," +
            " b.agent_id agentId," +
            " b.agent_name agentName," +
            " b.agent_staff_id agentStaffId," +
            " b.agent_staff_name agentStaffName," +
            " b.create_time createTime" +
            " FROM" +
            " bs_merchant a" +
            " LEFT JOIN bs_agent_mer b on b.mer_id = a.id and b.`status` <![CDATA[ <> ]]> 0" +
            " <if test='param.agentId != null'> and b.agent_id = #{param.agentId} </if>" +
            " where 1 = 1" +
            " <if test='param.isAgentStaffAllot != null and param.isAgentStaffAllot == true'> and b.agent_staff_id is not null and b.agent_staff_id = #{param.agentStaffId} and b.mer_id in (select mer_id from bs_agent_mer where agent_staff_id = #{param.agentStaffId} and `status` <![CDATA[ <> ]]> 0) </if>" +
            " <if test='param.isAgentStaffAllot != null and param.isAgentStaffAllot == false'> and b.agent_staff_id is null and b.mer_id not in (select mer_id from bs_agent_mer where agent_staff_id = #{param.agentStaffId} and `status` <![CDATA[ <> ]]> 0) </if>" +
            ")  a where 1 = 1" +
            " <if test='param.provinceCode != null'> and a.provinceCode = #{param.provinceCode} </if>" +
            " <if test='param.merSourceType != null'> and a.merSourceType = #{param.merSourceType} </if>" +
            " <if test='param.merNo != null'> and a.merNo like concat('%',#{param.merNo},'%') </if>" +
            " <if test='param.merName != null'> and a.merName like concat('%',#{param.merName},'%') </if>" +
            " <if test='param.isAgentAllot != null and param.isAgentAllot == true'> and a.agentId is not null </if>" +
            " <if test='param.isAgentAllot != null and param.isAgentAllot == false'> and a.agentId is null </if>" +
            " GROUP BY a.merId " +
            " </script> "})
/*    @Select("<script>" +
            " SELECT" +
            " a.agentMerId," +
            " a.merSourceType," +
            " a.provinceCode," +
            " a.provinceName," +
            " a.merNo," +
            " a.merName," +
            " a.merAddress," +
            " a.agentName," +
            " a.`status`," +
            " a.createTime" +
            " FROM" +
            " (" +
            " SELECT" +
            " a.id merId," +
            " a.source_type merSourceType," +
            " a.province_code provinceCode," +
            " a.province_name provinceName," +
            " a.mer_no merNo," +
            " a.mer_name merName," +
            " a.address merAddress," +
            " a.`status`," +
            " b.id agentMerId," +
            " b.agent_id agentId," +
            " b.agent_name agentName," +
            " b.agent_staff_id agentStaffId," +
            " b.agent_staff_name agentStaffName," +
            " b.create_time createTime " +
            " FROM" +
            " bs_merchant a" +
            " LEFT JOIN (select id,mer_id,agent_id,agent_name,agent_staff_id,agent_staff_name,create_time from bs_agent_mer " +
            " where agent_id = #{param.agentId} and `status` <![CDATA[ <> ]]> 0 " +
            " <if test='param.isAgentStaffAllot != null and param.isAgentStaffAllot == true'> and agent_staff_id = #{param.agentStaffId} and mer_id in (select mer_id from bs_agent_mer where agent_staff_id = #{param.agentStaffId} and `status` <![CDATA[ <> ]]> 0) </if>" +
            " <if test='param.isAgentStaffAllot != null and param.isAgentStaffAllot == false'> and mer_id not in (select mer_id from bs_agent_mer where agent_staff_id = #{param.agentStaffId} and `status` <![CDATA[ <> ]]> 0) </if>" +
            " GROUP BY mer_id) b on b.mer_id = a.id" +
            " WHERE a.`status` <![CDATA[ <> 0 ]]> ) a " +
            " WHERE 1 = 1 " +
            " <if test='param.provinceCode != null'> and a.provinceCode = #{param.provinceCode} </if>" +
            " <if test='param.merSourceType != null'> and a.merSourceType = #{param.merSourceType} </if>" +
            " <if test='param.merNo != null'> and a.merNo like concat('%',#{param.merNo},'%') </if>" +
            " <if test='param.merName != null'> and a.merName like concat('%',#{param.merName},'%') </if>" +
            " <if test='param.isAgentAllot != null and param.isAgentAllot == true'> and a.agentId is not null </if>" +
            " <if test='param.isAgentAllot != null and param.isAgentAllot == false'> and a.agentId is null </if>" +
            " <if test='param.status != null'> and a.status = #{param.status} </if>" +
            " </script>")*/
    List<AgentMerAllot> getAllotList(@Param("param") Map<String,Object> param);

    @Select("<script> " +
            " select a.mer_no gasNo from bs_agent_mer a " +
            " LEFT JOIN bs_merchant b on b.id = a.mer_id" +
            " where a.agent_id = #{agentId} and a.agent_staff_id is null and a.status = 1 and b.`status` = 1 GROUP BY a.mer_no" +
            " </script>")
    List<ResponseQueryGasInfoModel> getMerNoList(@Param("agentId") Long agentId);
}
