package com.yonge.cooleshow.teacher.controller;

import com.alibaba.fastjson.JSONObject;
import com.yonge.cooleshow.biz.dal.entity.Subject;
import com.yonge.cooleshow.biz.dal.entity.TeacherSubjectPrice;
import com.yonge.cooleshow.biz.dal.service.SubjectService;
import com.yonge.cooleshow.biz.dal.service.SysUserService;
import com.yonge.cooleshow.biz.dal.service.TeacherSubjectPriceService;
import com.yonge.cooleshow.biz.dal.wrapper.TeacherSubjectPriceWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Api(tags = "老师课程价格配置")
@RestController
@RequestMapping("${app-config.url.teacher:}/teacherSubjectPrice")
public class TeacherSubjectPriceController extends BaseController {
    @Resource
    private TeacherSubjectPriceService teacherSubjectPriceService;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SubjectService subjectService;

    @ApiOperation(value = "新增、修改老师课程价格配置")
    @PostMapping("/saveOrUpdate")
    public HttpResponseResult saveOrUpdate(@RequestBody List<TeacherSubjectPrice> teacherSubjectPriceList){
        if (CollectionUtils.isEmpty(teacherSubjectPriceList)) {
            throw new BizException("参数不能为空");
        }
        Long userId = sysUserService.getUserId();
        for (TeacherSubjectPrice e : teacherSubjectPriceList) {
            e.setTeacherId(userId);
            e.setId(null);
        }
        teacherSubjectPriceService.upSet(teacherSubjectPriceList);
        return succeed();
    }

    @ApiOperation(value = "删除老师课程价格配置")
    @PostMapping("/delete")
    public HttpResponseResult delete(Long id){
        teacherSubjectPriceService.lambdaUpdate()
                .eq(TeacherSubjectPrice::getTeacherId, sysUserService.getUserId())
                .eq(TeacherSubjectPrice::getId, id).remove();
        return succeed();
    }

    @ApiOperation(value = "获取老师课程价格配置")
    @PostMapping("/detail")
    public HttpResponseResult<TeacherSubjectPriceWrapper.TeacherSubjectPriceDto> detail(@RequestBody TeacherSubjectPriceWrapper.PriceSearch query){
        List<TeacherSubjectPrice> list = teacherSubjectPriceService.lambdaQuery()
                .eq(TeacherSubjectPrice::getTeacherId, sysUserService.getUserId())
                .eq(query.getSubjectId() != null, TeacherSubjectPrice::getSubjectId, query.getSubjectId())
                .eq(StringUtils.isNotEmpty(query.getCourseType()), TeacherSubjectPrice::getCourseType, query.getCourseType())
                .list();
        if(CollectionUtils.isNotEmpty(list)){
            //获取所有的声部、声部名称
            TeacherSubjectPriceWrapper.TeacherSubjectPriceDto dto = new TeacherSubjectPriceWrapper.TeacherSubjectPriceDto();
            String subjectIds = list.stream().map(e->e.getSubjectId().toString()).distinct().collect(Collectors.joining(","));
            String subjectNames = list.stream().map(TeacherSubjectPrice::getSubjectName).distinct().collect(Collectors.joining(","));
            dto.setSubjectIds(subjectIds);
            dto.setSubjectName(subjectNames);
            dto.setTeacherId(list.get(0).getTeacherId());
            dto.setCourseType(list.get(0).getCourseType());
            dto.setFreeMinutes(list.get(0).getFreeMinutes());
            dto.setCourseMinutes(list.get(0).getCourseMinutes());
            dto.setSubjectPrice(list.get(0).getSubjectPrice());
            return succeed(dto);
        }
        return succeed();
    }

    @ApiOperation(value = "获取老师课程价格配置")
    @PostMapping("/list")
    public HttpResponseResult<List<TeacherSubjectPriceWrapper.TeacherSubjectPriceDto>> list(@RequestBody TeacherSubjectPriceWrapper.PriceSearch query){
        List<TeacherSubjectPrice> list = teacherSubjectPriceService.lambdaQuery()
                .eq(TeacherSubjectPrice::getTeacherId, sysUserService.getUserId())
                .eq(query.getSubjectId() != null, TeacherSubjectPrice::getSubjectId, query.getSubjectId())
                .eq(StringUtils.isNotEmpty(query.getCourseType()), TeacherSubjectPrice::getCourseType, query.getCourseType())
                .list();
        if(CollectionUtils.isNotEmpty(list)){
            List<TeacherSubjectPriceWrapper.TeacherSubjectPriceDto> results = JSONObject.parseArray(JSONObject.toJSONString(list), TeacherSubjectPriceWrapper.TeacherSubjectPriceDto.class);
            List<Long> subjectIds = list.stream().map(TeacherSubjectPrice::getSubjectId).distinct().collect(Collectors.toList());
            List<Subject> subjectList = subjectService.findBySubjectByIdList(subjectIds);
            Map<Long, String> subjectMap = subjectList.stream().collect(Collectors.toMap(Subject::getId, Subject::getImg));
            results.forEach(e->e.setSubjectPic(subjectMap.get(e.getSubjectId())));
            return succeed(results);
        }
        return succeed();
    }
}

