package com.miaxis.coach.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miaxis.coach.domain.CoachEvaluate;
import com.miaxis.coach.domain.CoachEvaluateImages;
import com.miaxis.coach.domain.CoachEvaluateLabel;
import com.miaxis.coach.dto.CoachEvaluateDto;
import com.miaxis.coach.mapper.CoachEvaluateLabelMapper;
import com.miaxis.coach.mapper.CoachEvaluateMapper;
import com.miaxis.coach.service.ICoachEvaluateImagesService;
import com.miaxis.coach.service.ICoachEvaluateService;
import com.miaxis.common.core.domain.Response;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


/**
 * 教练评价Service业务层处理
 *
 * @author wwl
 * @date 2020-12-28
 */
@Service
public class CoachEvaluateServiceImpl extends ServiceImpl<CoachEvaluateMapper, CoachEvaluate> implements ICoachEvaluateService {

    @Resource
    private CoachEvaluateMapper coachEvaluateMapper;

    @Resource
    private CoachEvaluateLabelMapper coachEvaluateLabelMapper;

    @Resource
    private ICoachEvaluateImagesService coachEvaluateImagesService;


    /**
     * 查询教练评价标签列表
     * @return
     */
    @Override
    public List<CoachEvaluateLabel> selectCoachEvaluateLabelList() {
        QueryWrapper<CoachEvaluateLabel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status","0");
        return coachEvaluateLabelMapper.selectList(queryWrapper);
    }

    /**
     * 新增教练评价
     * @param coachEvaluateDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Response addCoachEvaluate(CoachEvaluateDto coachEvaluateDto) {
        try{
            //保存评价
            CoachEvaluate coachEvaluate = new CoachEvaluate();
            coachEvaluate.setUserId(Long.valueOf(coachEvaluateDto.getUserId()));
            coachEvaluate.setCoachnum(coachEvaluateDto.getCoachnum());
            coachEvaluate.setAreaStars(Long.valueOf(coachEvaluateDto.getAreaStars()));
            coachEvaluate.setTotelStars(coachEvaluateDto.getTotalStars());
            coachEvaluate.setContent(coachEvaluateDto.getContent());
            coachEvaluate.setLabelIds(coachEvaluateDto.getLabelIds());
            coachEvaluate.setStatus(0);
            coachEvaluate.setIsAnonymous(Integer.valueOf(coachEvaluateDto.getIsAnonymous()));
            coachEvaluateMapper.insert(coachEvaluate);

            //保存照片
            ArrayList<CoachEvaluateImages> evaluateImages = new ArrayList<>();
            JSONArray jsonArray = JSONArray.parseArray(coachEvaluateDto.getImagesJson());
            System.out.println(jsonArray);
            List<Map> lists = jsonArray.toJavaList(Map.class);
            for (Map list : lists) {
                CoachEvaluateImages images = new CoachEvaluateImages();
                images.setCoachEvaluateId(coachEvaluate.getId());
                images.setImagePath(list.get("imagePath").toString());
                images.setImageUrl(list.get("imageUrl").toString());
                evaluateImages.add(images);
            }
            coachEvaluateImagesService.saveBatch(evaluateImages);

            return Response.success();
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }

    /**
     * pc 查询列表
     */
    @Override
    public List<CoachEvaluate> queryList(CoachEvaluate coachEvaluate) {
        LambdaQueryWrapper<CoachEvaluate> lqw = Wrappers.lambdaQuery();
        if (coachEvaluate.getUserId() != null){
            lqw.eq(CoachEvaluate::getUserId ,coachEvaluate.getUserId());
        }
        if (StringUtils.isNotBlank(coachEvaluate.getCoachnum())){
            lqw.eq(CoachEvaluate::getCoachnum ,coachEvaluate.getCoachnum());
        }
        if (coachEvaluate.getAttitudeStars() != null){
            lqw.eq(CoachEvaluate::getAttitudeStars ,coachEvaluate.getAttitudeStars());
        }
        if (coachEvaluate.getAreaStars() != null){
            lqw.eq(CoachEvaluate::getAreaStars ,coachEvaluate.getAreaStars());
        }
        if (StringUtils.isNotBlank(coachEvaluate.getContent())){
            lqw.eq(CoachEvaluate::getContent ,coachEvaluate.getContent());
        }
        if (StringUtils.isNotBlank(coachEvaluate.getLabelIds())){
            lqw.eq(CoachEvaluate::getLabelIds ,coachEvaluate.getLabelIds());
        }
        if (coachEvaluate.getTotelStars() != null){
            lqw.eq(CoachEvaluate::getTotelStars ,coachEvaluate.getTotelStars());
        }
        if (coachEvaluate.getIsAnonymous() != null){
            lqw.eq(CoachEvaluate::getIsAnonymous ,coachEvaluate.getIsAnonymous());
        }
        if (coachEvaluate.getStatus() != null){
            lqw.eq(CoachEvaluate::getStatus ,coachEvaluate.getStatus());
        }
        return this.list(lqw);
    }


}
