package com.ym.mec.web.controller.education;


import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.EmployeeDao;
import com.ym.mec.biz.dal.dto.BasicUserDto;
import com.ym.mec.biz.dal.entity.*;
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.dal.page.RepairStudentQueryInfo;
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 com.ym.mec.common.page.QueryInfo;
import com.ym.mec.util.date.DateUtil;
import io.swagger.annotations.Api;
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.Date;
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 EmployeeDao employeeDao;
    @Autowired
    private StudentRepairService studentRepairService;
    @Autowired
    private StudentPaymentOrderService studentPaymentOrderService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsCategoryService goodsCategoryService;
    @Autowired
    private StudentGoodsSellService studentGoodsSellService;

    @ApiOperation("获取学生列表")
    @GetMapping(value = "/getStudents")
    public HttpResponseResult<PageInfo<BasicUserDto>> getStudents(RepairStudentQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        Employee employee = employeeDao.get(sysUser.getId());
        if (Objects.isNull(employee)) {
            return failed(HttpStatus.NOT_ACCEPTABLE, "员工信息不存在");
        }
        queryInfo.setEmployeeId(sysUser.getId());
        queryInfo.setOrganIdList(employee.getOrganIdList());
        return succeed(studentRepairService.getStudents(queryInfo));
    }

    @ApiOperation("获取商品购买教务老师关联的学员列表")
    @GetMapping(value = "/queryEduStudents")
    public HttpResponseResult<PageInfo<BasicUserDto>> queryEduStudents(QueryInfo queryInfo) {
        return succeed(studentRepairService.queryEduStudents(queryInfo));
    }

    @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, "恭喜您,购买成功!");
        }
        return succeed(map);
    }

    @ApiOperation("添加维修单")
    @PostMapping(value = "/addRepair")
    public HttpResponseResult addRepair(StudentRepair repairInfo) throws Exception {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        repairInfo.setEmployeeId(sysUser.getId());
        repairInfo.setEmployeeName(sysUser.getRealName());
        return succeed(studentRepairService.addRepair(repairInfo));
    }

    @ApiOperation("获取学生的维修记录")
    @GetMapping(value = "/getStudentRepairList")
    public HttpResponseResult getStudentRepairList(RepairStudentQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        queryInfo.setEmployeeId(sysUser.getId());
        queryInfo.setPayStatus(2);
        return succeed(studentRepairService.queryPage(queryInfo));
    }

    @ApiOperation("获取乐器种类")
    @GetMapping(value = "/findSubSubjects")
    public HttpResponseResult findSubSubjects() {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        return succeed(subjectService.findSubSubjects(1));
    }

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

    @ApiOperation("获取维修记录详情")
    @GetMapping(value = "/getRepairInfo")
    public HttpResponseResult getRepairInfo(Integer id) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        return succeed(studentRepairService.getRepairInfo(id));
    }


    @ApiOperation("获取维修列表")
    @GetMapping(value = "/queryPage")
    public HttpResponseResult queryPage(RepairStudentQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        queryInfo.setEmployeeId(sysUser.getId());
        Date endTime = queryInfo.getEndTime();
        if(endTime != null){
            queryInfo.setEndTime(DateUtil.addDays(endTime, 1));
        }
        queryInfo.setPayStatus(2);
        return succeed(studentRepairService.queryPage(queryInfo));
    }


    @ApiOperation("维修完成")
    @PostMapping(value = "/repairSuccess")
    public HttpResponseResult repairSuccess(Integer id,String description,Integer repairStatus) {
        studentRepairService.repairSuccess(id,description,repairStatus);
        return succeed();
    }

    @ApiOperation(value = "分页查询商品(教材、辅件)列表")
    @GetMapping("/queryGoodsPage")
    public Object queryPage(GoodsQueryInfo queryInfo){
        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));
    }
}
