package com.qxgmat.service.inline;

import com.github.pagehelper.Page;
import com.nuliji.tools.AbstractService;
import com.nuliji.tools.Transform;
import com.nuliji.tools.exception.ParameterException;
import com.nuliji.tools.exception.SystemException;
import com.nuliji.tools.mybatis.Example;
import com.qxgmat.data.constants.enums.status.AskStatus;
import com.qxgmat.data.constants.enums.status.DirectionStatus;
import com.qxgmat.data.constants.enums.user.AskTarget;
import com.qxgmat.data.constants.enums.user.MoneyRange;
import com.qxgmat.data.dao.UserAskQuestionMapper;
import com.qxgmat.data.dao.entity.QuestionNo;
import com.qxgmat.data.dao.entity.UserAskQuestion;
import com.qxgmat.data.relation.UserAskQuestionRelationMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class UserAskQuestionService extends AbstractService {
    private static final Logger logger = LoggerFactory.getLogger(UserAskQuestionService.class);

    @Resource
    private UserAskQuestionMapper userAskQuestionMapper;

    @Resource
    private UserAskQuestionRelationMapper userAskQuestionRelationMapper;

    public Page<UserAskQuestion> listWithUser(int page, int size, String type, Number category, Number userId, Number questionNoId, AskTarget target, AskStatus status, Integer showStatus, MoneyRange moneyRange, String order, DirectionStatus direction){
        String tk = target != null ? target.key : "";
        Integer statusIndex = status != null ? status.index : null;
        Integer max = moneyRange != null ? moneyRange.max == Integer.MAX_VALUE ? null : moneyRange.max : null;
        Integer min = moneyRange != null ? moneyRange.min : null;
        if(order.isEmpty()) order = "id";
        if (direction == null){
            direction = DirectionStatus.DESC;
        }
        String finalOrder = order;
        DirectionStatus finalDirection = direction;
        Page<UserAskQuestion> p = page(
                ()-> userAskQuestionRelationMapper.listWithUser(type, category, userId, questionNoId, tk, statusIndex, showStatus, min, max, finalOrder, finalDirection.key)
        , page, size);

        Collection ids = Transform.getIds(p, UserAskQuestion.class, "id");
        Transform.replace(p, select(ids), UserAskQuestion.class, "id");
        return p;
    }

    public List<UserAskQuestion> listByQuestion(Number questionId, Boolean showStatus){
        Example example = new Example(UserAskQuestion.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("questionId", questionId)

        );
        if (showStatus != null)
            example.and(
                    example.createCriteria()
                            .andEqualTo("showStatus", showStatus?1:0)
                            .andEqualTo("answerStatus", 1)

            );
        example.orderBy("order").desc();
        return select(userAskQuestionMapper, example);
    }

    /**
     * 根据列表顺序全部更新排序：从大到小
     * @param ids
     */
    @Transactional
    public void updateOrder(Integer[] ids){
        int order = ids.length;
        List<UserAskQuestion> UserAskQuestionList = select(userAskQuestionMapper, ids);
        Map UserAskQuestionMap = Transform.getMap(UserAskQuestionList, UserAskQuestion.class, "id");
        for (Integer id : ids){
            UserAskQuestion UserAskQuestion = (UserAskQuestion)UserAskQuestionMap.get(id);
            if (UserAskQuestion == null) continue;
            if (UserAskQuestion.getOrder() == order) continue;
            update(userAskQuestionMapper, UserAskQuestion.builder()
                    .id(id)
                    .order(order)
                    .build()
            );
            order -= 1;
        }
    }

//    @Transactional
//    public void order(Number id, Integer order){
//        UserAskQuestion entity = get(id);
//        adjust(entity.getQuestionId(), entity.getOrder(), ">", "-=");
//        adjust(entity.getQuestionId(), order, ">=", "+=");
//        entity.setOrder(order);
//    }

    public UserAskQuestion add(UserAskQuestion message){
        int result = insert(userAskQuestionMapper, message);
        message = one(userAskQuestionMapper, message.getId());
        if(message == null){
            throw new SystemException("提问添加失败");
        }
        return message;
    }

    public UserAskQuestion edit(UserAskQuestion message){
        UserAskQuestion in = one(userAskQuestionMapper, message.getId());
        if(in == null){
            throw new ParameterException("提问不存在");
        }
        int result = update(userAskQuestionMapper, message);
        return message;
    }

    public boolean delete(Number id){
        UserAskQuestion in = one(userAskQuestionMapper, id);
        if(in == null){
            throw new ParameterException("提问不存在");
        }
        int result = delete(userAskQuestionMapper, id);
        return result > 0;
    }

    public UserAskQuestion get(Number id){
        UserAskQuestion in = one(userAskQuestionMapper, id);

        if(in == null){
            throw new ParameterException("提问不存在");
        }
        return in;
    }

    public Page<UserAskQuestion> select(int page, int pageSize){
        return select(userAskQuestionMapper, page, pageSize);
    }

    public List<UserAskQuestion> select(Collection ids){
        return select(userAskQuestionMapper, ids);
    }

}
