package com.boman.web.core.mapper;

import com.alibaba.fastjson.JSONObject;
import com.boman.common.core.utils.SqlProviderUtils;
import com.boman.domain.jflow.ProcessNodeCandidator;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

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

import static com.boman.common.core.utils.obj.ObjectUtils.isNotEmpty;
import static com.boman.domain.constant.FormDataConstant.EQ;

@Mapper
@Component
public interface MessageMapper {

    Logger LOGGER = LoggerFactory.getLogger(MessageMapper.class);

    /**
     * 功能描述: 自定义查询，需要查询的字段和value都在condition中
     * {@link MessageMapper.SqlProvider#countByCondition(Map)}
     *
     * @param username username
     * @return int
     */
    @SelectProvider(type = MessageMapper.SqlProvider.class, method = "countByCondition")
    int countByCondition(@Param("username") String username);

    /**
     * 功能描述: 查询
     * {@link MessageMapper.SqlProvider#selectByCondition(Map)}
     *
     * @param username username
     * @return int
     */
    @SelectProvider(type = MessageMapper.SqlProvider.class, method = "selectByCondition")
    List<JSONObject> toDoMessage(@Param("username") String username
            , @Param("limit") int limit
            , @Param("offset") int offset);

    /**
     * 功能描述: 查询
     * {@link MessageMapper.SqlProvider#getIndexCountInfo(Map)}
     *
     * @param userId userId
     * @return int
     */
    @SelectProvider(type = MessageMapper.SqlProvider.class, method = "getIndexCountInfo")
    Long getIndexCountInfo(@Param("userId") Long userId, @Param("dateStr") String dateStr);

    /**
     * 功能描述: 统计是否完成全部已读
     *
     * {@link MessageMapper.SqlProvider#countReceive(Map)}
     *
     * @param messageId userId
     * @return int
     */
    @SelectProvider(type = MessageMapper.SqlProvider.class, method = "countReceive")
    Integer countReceive(@Param("messageId") Long messageId);

    /**
     * {@link MessageMapper.SqlProvider#updateReceive(Map)}
     *
     * @param dataId
     * @param receiveTime
     * @return
     */
    @UpdateProvider(type = MessageMapper.SqlProvider.class, method = "updateReceive")
    int updateReceive(@Param("dataId") Long dataId, @Param("receiveTime") String receiveTime);

    /**
     * {@link MessageMapper.SqlProvider#updateMessageSituationStatus(Map)}
     *
     * @param dataId
     * @return
     */
    @UpdateProvider(type = MessageMapper.SqlProvider.class, method = "updateMessageSituationStatus")
    int updateMessageSituationStatus(@Param("dataId") Long dataId);

    class SqlProvider {

        public String countByCondition(Map<String, Object> para) {
            String result = "SELECT count( 1 )  FROM boman_message t " +
                    " JOIN urge_read_message u ON u.message_id = t.id  AND u.receive_user_name = '"  + para.get("username") +  "' " +
                    " join boman_message_receive r on r.receive_user_name = u.receive_user_name and r.message_id = t.id WHERE t.is_del = 'N' and r.`status` = 'N' ";
            LOGGER.info("查询count拼出的sql语句为: {} \r\n ", result);
            return result;
        }

        public String selectByCondition(Map<String, Object> para) {
            String result = "SELECT t.message_title, t.message_code, t.send_user_id, t.send_user_name, t.create_dept_name, r.send_time as receive_time, t.id,r.from_dept_name, "
                    + " t.finish_time, r.id as receive_id FROM boman_message t JOIN urge_read_message u ON u.message_id = t.id  AND u.receive_user_name = '" + para.get("username") + "' "
                    + " JOIN boman_message_receive r on r.receive_user_name = u.receive_user_name and r.message_id = t.id "
                    + " WHERE t.is_del = 'N'  and r.`status` = 'N' ORDER BY t.create_time DESC  LIMIT " + para.get("limit") + "," + para.get("offset");

            LOGGER.info("查询拼出的sql语句为：{} \r\n", result);
            return result;
        }

        public String getIndexCountInfo(Map<String, Object> para) {
            String result = "SELECT count(1)  FROM boman_message  WHERE send_user_id = " + para.get("userId") + "  " +
                    " AND DATE_FORMAT(create_time, '%Y-%m-%d') = '" + para.get("dateStr") + "'  AND message_situation <> '3'";

            LOGGER.info("查询拼出的sql语句为：{} \r\n", result);
            return result;
        }

        public String countReceive(Map<String, Object> para) {
            String result = "select count(1) from boman_message_receive t where t.visible  = 'Y' and status = 'N' and t.message_id = " + para.get("messageId");
            return result;
        }

        public String updateReceive(Map<String, Object> para) {
            String sql = "update boman_message_receive set status = 'Y', receive_time = str_to_date('" + para.get("receiveTime") + "', '%Y-%m-%d %H:%i:%s') where id = " + para.get("dataId");
            return sql;
        }

        public String updateMessageSituationStatus(Map<String, Object> para) {
            String sql = "update boman_message set message_situation = '2' where id = " + para.get("dataId");
            return sql;
        }
    }
}
