package com.ym.mec.teacher.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dto.BasicUserDto;
import com.ym.mec.biz.dal.entity.StudentGoodsSell;
import com.ym.mec.biz.dal.entity.StudentPaymentOrder;
import com.ym.mec.biz.dal.entity.Teacher;
import com.ym.mec.biz.dal.enums.DealStatusEnum;
import com.ym.mec.biz.dal.enums.GroupType;
import com.ym.mec.biz.dal.enums.OrderTypeEnum;
import com.ym.mec.biz.dal.page.GoodsCategoryQueryInfo;
import com.ym.mec.biz.dal.page.GoodsQueryInfo;
import com.ym.mec.biz.dal.page.GoodsSellQueryInfo;
import com.ym.mec.biz.service.*;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.page.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@RequestMapping("eduRepair")
@Api(tags = "教务维修服务")
@RestController
public class EduRepairController extends BaseController {

    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private StudentRepairService studentRepairService;
    @Autowired
    private StudentPaymentOrderService studentPaymentOrderService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsCategoryService goodsCategoryService;
    @Autowired
    private StudentGoodsSellService studentGoodsSellService;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "search", dataType = "String", value = "关键字"),
            @ApiImplicitParam(name = "page", dataType = "Integer", value = "页数"),
            @ApiImplicitParam(name = "rows", dataType = "Integer", value = "每页数量"),
    })
    @ApiOperation("查询老师主分部下所有学生")
    @PostMapping(value = "/getStudentsByTeacherOrgan")
    public HttpResponseResult<PageInfo<BasicUserDto>> getStudentsByTeacherOrgan(@RequestBody Map<String, Object> param) {
        return succeed(studentRepairService.getStudentsByTeacherOrgan(param));
    }

    @ApiOperation("添加商品销售订单")
    @PostMapping(value = "/addGoodsSellOrder")
    public HttpResponseResult addGoodsSellOrder(@RequestBody StudentGoodsSell studentGoodsSell) throws Exception {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            throw new BizException("请登录");
        }
        if (studentGoodsSell.getIsRepeatPay() == false) {
            List<StudentPaymentOrder> list = studentPaymentOrderService.queryByCondition(GroupType.GOODS_SELL, null, studentGoodsSell.getUserId(), DealStatusEnum.ING,
                    OrderTypeEnum.RENEW);
            if (list.size() > 0) {
                return failed(HttpStatus.CONTINUE, "该学员有待支付的订单");
            }
        }
        studentGoodsSell.setTeacherId(sysUser.getId());
        studentGoodsSell.setAuthorUser(sysUser.getId());
        Map map = studentRepairService.addGoodsSellOrder(studentGoodsSell);
        if (map.containsKey("tradeState")) {
            return failed(HttpStatus.CREATED, map, "恭喜您,购买成功!");
        }
        return succeed(map);
    }

    @ApiOperation("获取学生信息")
    @GetMapping(value = "/getStudentInfo")
    public HttpResponseResult getStudentInfo(Integer studentId) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        return succeed(studentRepairService.getStudentInfo(studentId));
    }

    @ApiOperation(value = "分页查询商品(教材、辅件)列表")
    @GetMapping("/queryGoodsPage")
    public Object queryPage(GoodsQueryInfo queryInfo) {
        queryInfo.setOrganId(null);
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        Teacher teacher = teacherService.get(sysUser.getId());
        if (queryInfo.getStudentShowOrganId() == null &&
                queryInfo.getEducationShowOrganId() == null &&
                queryInfo.getCourseFeeShowOrganId() == null &&
                queryInfo.getMemberFeeShowOrganId() == null &&
                queryInfo.getReplacementShowOrganId() == null) {
            queryInfo.setEducationShowOrganId(teacher.getOrganId().toString());
        } else if (Objects.isNull(teacher.getOrganId())) {
            return failed("用户所在分部异常");
        }
        return succeed(goodsService.queryPage(queryInfo));
    }

    @ApiOperation(value = "分页查询商品分类列表")
    @GetMapping("/queryGoodsCategoryPage")
    public Object queryGoodsCategoryPage(GoodsCategoryQueryInfo queryInfo) {
        return succeed(goodsCategoryService.queryPage(queryInfo));
    }

    @ApiOperation(value = "分页查询教务老师关联的学员商品订单")
    @GetMapping("/queryStudentGoodsOrders")
    public Object queryStudentGoodsOrders(GoodsSellQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        queryInfo.setTeacherId(sysUser.getId());
        return succeed(studentGoodsSellService.queryStudentGoodsOrders(queryInfo));
    }

}
