package com.miaxis.app.school.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.school.domain.SchoolEvaluate;
import com.miaxis.app.school.domain.SchoolEvaluateImages;
import com.miaxis.app.school.domain.SchoolEvaluateLabel;
import com.miaxis.app.school.dto.SchoolEvaluateDto;
import com.miaxis.app.school.mapper.SchoolEvaluateLabelMapper;
import com.miaxis.app.school.mapper.SchoolEvaluateMapper;
import com.miaxis.app.school.service.ISchoolEvaluateImagesService;
import com.miaxis.app.school.service.ISchoolEvaluateService;
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 SchoolEvaluateServiceImpl extends ServiceImpl<SchoolEvaluateMapper, SchoolEvaluate> implements ISchoolEvaluateService {

    @Resource
    private SchoolEvaluateMapper schoolEvaluateMapper;

    @Resource
    private SchoolEvaluateLabelMapper schoolEvaluateLabelMapper;

    @Resource
    private ISchoolEvaluateImagesService schoolEvaluateImagesService;


    /**
     * 查询驾校评价标签列表
     * @return
     */
    @Override
    public List<SchoolEvaluateLabel> selectSchoolEvaluateLabelList() {
        QueryWrapper<SchoolEvaluateLabel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status","0");
        return schoolEvaluateLabelMapper.selectList(queryWrapper);
    }

    /**
     * 新增驾校评价
     * @param schoolEvaluateDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Response addSchoolEvaluate(SchoolEvaluateDto schoolEvaluateDto) {
        try {
            //保存评价
            SchoolEvaluate schoolEvaluate = new SchoolEvaluate();
            schoolEvaluate.setUserId(Long.valueOf(schoolEvaluateDto.getUserId()));
            schoolEvaluate.setInscode(schoolEvaluateDto.getInscode());
            schoolEvaluate.setAreaStarts(Long.valueOf(schoolEvaluateDto.getAreaStarts()));
            schoolEvaluate.setAttitudeStarts(schoolEvaluateDto.getAttitudeStarts());
            schoolEvaluate.setTotalStarts(schoolEvaluateDto.getTotalStarts());
            schoolEvaluate.setContent(schoolEvaluateDto.getContent());
            schoolEvaluate.setSchoolLabelIds(schoolEvaluateDto.getSchoolLabelIds());
            schoolEvaluate.setIsAnonymous(Long.valueOf(schoolEvaluateDto.getIsAnonymous()));
            schoolEvaluate.setStatus(0);
            schoolEvaluateMapper.insert(schoolEvaluate);

            //保存照片
            ArrayList<SchoolEvaluateImages> evaluateImages = new ArrayList<>();
            JSONArray jsonArray = JSONArray.parseArray(schoolEvaluateDto.getImagesJson());
            List<Map> lists = jsonArray.toJavaList(Map.class);
            for (Map list : lists) {
                SchoolEvaluateImages images = new SchoolEvaluateImages();
                images.setSchoolEvaluateId(schoolEvaluate.getId());
                images.setImagePath(list.get("imagePath").toString());
                images.setImageUrl(list.get("imageUrl").toString());
                evaluateImages.add(images);
            }
            schoolEvaluateImagesService.saveBatch(evaluateImages);

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


}
