package com.sqx.modules.errand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.errand.dao.ErrandEvaluateDao;
import com.sqx.modules.errand.dao.TbIndentDao;
import com.sqx.modules.errand.entity.ErrandEvaluate;
import com.sqx.modules.errand.entity.TbIndent;
import com.sqx.modules.errand.service.ErrandEvaluateService;
import com.sqx.modules.errand.service.TbIndentService;
import com.sqx.modules.order.entity.Evaluate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ErrandEvaluateServiceImpl extends ServiceImpl<ErrandEvaluateDao, ErrandEvaluate> implements ErrandEvaluateService {

    @Autowired
    private ErrandEvaluateDao errandEvaluateDao;
    @Autowired
    private TbIndentDao tbIndentDao;
    @Autowired
    private UserService userService;
    @Autowired
    private TbIndentService indentService;

    @Override
    public Result userEvaluate(Long userId, ErrandEvaluate evaluate) {
        TbIndent indent = tbIndentDao.selectIndentByIndentNumber(evaluate.getIndentNumber());
        evaluate.setUserId(userId);
        evaluate.setRiderUserId(indent.getRiderUserId());
        evaluate.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        errandEvaluateDao.insert(evaluate);
        return Result.success();
    }

    @Override
    public IPage<ErrandEvaluate> getMyEvaluateList(Long userId, Integer page, Integer limit) {
        Page<ErrandEvaluate> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
        }
        return errandEvaluateDao.selectPage(pages, new QueryWrapper<ErrandEvaluate>().eq("user_id", userId));
    }

    @Override
    public Result selectEvaluate(Integer page, Integer limit,String nickName,String riderName,Long userId,Long riderUserId,String satisfactionFlag,String indentNumber) {
        Page<ErrandEvaluate> pages = new Page<>(page, limit);
        IPage<ErrandEvaluate> evaluateIPage = baseMapper.selectEvaluateList(pages, nickName, riderName, userId,riderUserId, satisfactionFlag, indentNumber);
        List<ErrandEvaluate> records = evaluateIPage.getRecords();
        for(ErrandEvaluate evaluate:records){
            evaluate.setUser(userService.getById(evaluate.getUserId()));
            evaluate.setRiderUser(userService.getById(evaluate.getRiderUserId()));
            evaluate.setIndent(indentService.findIndentByIndentNumber(evaluate.getIndentNumber()));
        }
        return Result.success().put("data", evaluateIPage);
    }

}
