package com.miaxis.app.coach.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miaxis.app.coach.domain.CoachEvaluate;
import com.miaxis.app.coach.domain.CoachEvaluateImages;
import com.miaxis.app.coach.domain.CoachEvaluateLabel;
import com.miaxis.app.coach.dto.CoachEvaluateDto;
import com.miaxis.app.coach.mapper.CoachEvaluateLabelMapper;
import com.miaxis.app.coach.mapper.CoachEvaluateMapper;
import com.miaxis.app.coach.service.ICoachEvaluateImagesService;
import com.miaxis.app.coach.service.ICoachEvaluateService;
import com.miaxis.common.core.domain.Response;
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 miaxis
 * @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.setAttitudeStarts(coachEvaluateDto.getAttitudeStars());
            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);
        }

    }

}
