package com.uas.platform.b2b.manage.service.impl;

import com.alibaba.fastjson.JSON;
import com.uas.platform.b2b.manage.core.util.StringUtils;
import com.uas.platform.b2b.manage.dao.ErrorLogDao;
import com.uas.platform.b2b.manage.dao.NotExistOrdersDao;
import com.uas.platform.b2b.manage.dao.NotExistProductDao;
import com.uas.platform.b2b.manage.model.ErrorLog;
import com.uas.platform.b2b.manage.service.ErrorLogService;
import com.uas.platform.core.model.PageInfo;
import com.uas.platform.core.persistence.criteria.CriterionExpression;
import com.uas.platform.core.persistence.criteria.LogicalExpression;
import com.uas.platform.core.persistence.criteria.PredicateUtils;
import com.uas.platform.core.persistence.criteria.SimpleExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Date;

/**
 * B2B接口错误日志记录
 *
 * Created by hejq on 2018-06-21.
 */
@Service
public class ErrorLogServiceImpl implements ErrorLogService {

    @Autowired
    private ErrorLogDao logDao;

    /**
     * UU号正则表达式
     */
    static final String UU_REGEXP = "^\\d{4,}$";

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private NotExistOrdersDao notExistOrdersDao;

    @Autowired
    private NotExistProductDao notExistProductDao;

    /**
     * 通过关键词分页查询B2B接口错误日志
     *
     * @param info     分页信息
     * @param keyword  关键字
     * @param fromDate 起始时间
     * @param endDate  截止时间
     * @return
     */
    @Override
    public Page<ErrorLog> findErrLogsByPageInfo(final PageInfo info, String keyword, Long fromDate, Long endDate) {
        if (!StringUtils.isEmpty(keyword)) {
            SimpleExpression enName = new SimpleExpression("enName", keyword, CriterionExpression.Operator.LIKE, true);
            if (keyword.matches(UU_REGEXP)) {
                SimpleExpression enUU = new SimpleExpression("enUU", keyword, CriterionExpression.Operator.EQ, true);
                SimpleExpression[] expressions = new SimpleExpression[]{enUU, enName};
                LogicalExpression logical = new LogicalExpression(expressions, CriterionExpression.Operator.OR);
                info.expression(logical);
            } else {
                SimpleExpression[] expressions = new SimpleExpression[]{enName};
                LogicalExpression logical = new LogicalExpression(expressions, CriterionExpression.Operator.OR);
                info.expression(logical);
            }
            if (null != fromDate) {
                info.expression(PredicateUtils.gte("date", new Date(fromDate), false));
            }
            if (null != endDate) {
                info.expression(PredicateUtils.lte("date", new Date(endDate), false));
            }
        }
        return logDao.findAll(new Specification<ErrorLog>() {
            @Override
            public Predicate toPredicate(Root<ErrorLog> root, CriteriaQuery<?> query,
                                         CriteriaBuilder builder) {
                return query.where(info.getPredicates(root, query, builder)).getRestriction();
            }
        }, info);
    }

    /**
     * 通过主表id查询参数详情
     *
     * @param id 主表id
     * @return
     */
    @Override
    public String findParameters(Long id) {
        return jdbcTemplate.queryForObject("select log_parameters from log$error$detail where log_logid = " + id, String.class);
    }

    /**
     * 通过标识和类型查询信息
     *
     * @param sign 标识
     * @param msg  参数
     * @return
     */
    @Override
    public ModelMap findNotExistDataBySign(String sign, String msg) {
        // 未找到物料信息
        String NOT_EXIST_PRODUCTS = "物料";
        if (msg.contains(NOT_EXIST_PRODUCTS)) {
            return new ModelMap("content", JSON.toJSONString(notExistProductDao.findBySign(sign)));
        } else {
            return new ModelMap("content", JSON.toJSONString(notExistOrdersDao.findBySign(sign)));
        }
    }

}
