package com.miaxis.school.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.school.domain.SchoolEvaluate;
import com.miaxis.school.domain.SchoolEvaluateImages;
import com.miaxis.school.domain.SchoolEvaluateLabel;
import com.miaxis.school.dto.SchoolEvaluateDto;
import com.miaxis.school.mapper.SchoolEvaluateLabelMapper;
import com.miaxis.school.mapper.SchoolEvaluateMapper;
import com.miaxis.school.service.ISchoolEvaluateImagesService;
import com.miaxis.school.service.ISchoolEvaluateService;
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 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(0l);
            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);
        }
    }

    /**
     * pc 查询列表
     */
    @Override
    public List<SchoolEvaluate> queryList(SchoolEvaluate schoolEvaluate) {
        LambdaQueryWrapper<SchoolEvaluate> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(schoolEvaluate.getInscode())){
            lqw.eq(SchoolEvaluate::getInscode ,schoolEvaluate.getInscode());
        }
        if (StringUtils.isNotBlank(schoolEvaluate.getContent())){
            lqw.eq(SchoolEvaluate::getContent ,schoolEvaluate.getContent());
        }
        if (schoolEvaluate.getAttitudeStarts() != null){
            lqw.eq(SchoolEvaluate::getAttitudeStarts ,schoolEvaluate.getAttitudeStarts());
        }
        if (schoolEvaluate.getAreaStarts() != null){
            lqw.eq(SchoolEvaluate::getAreaStarts ,schoolEvaluate.getAreaStarts());
        }
        if (schoolEvaluate.getUserId() != null){
            lqw.eq(SchoolEvaluate::getUserId ,schoolEvaluate.getUserId());
        }
        if (schoolEvaluate.getTotalStarts() != null){
            lqw.eq(SchoolEvaluate::getTotalStarts ,schoolEvaluate.getTotalStarts());
        }
        if (schoolEvaluate.getIsAnonymous() != null){
            lqw.eq(SchoolEvaluate::getIsAnonymous ,schoolEvaluate.getIsAnonymous());
        }
        if (StringUtils.isNotBlank(schoolEvaluate.getSchoolLabelIds())){
            lqw.eq(SchoolEvaluate::getSchoolLabelIds ,schoolEvaluate.getSchoolLabelIds());
        }
        if (schoolEvaluate.getStatus() != null){
            lqw.eq(SchoolEvaluate::getStatus ,schoolEvaluate.getStatus());
        }
        return this.list(lqw);
    }

}
