package com.ym.mec.student.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
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 com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.StudentGoodsSell;
import com.ym.mec.biz.dal.entity.StudentPaymentOrder;
import com.ym.mec.biz.dal.entity.StudentRepair;
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.GoodsCategoryService;
import com.ym.mec.biz.service.GoodsService;
import com.ym.mec.biz.service.StudentGoodsSellService;
import com.ym.mec.biz.service.StudentPaymentOrderService;
import com.ym.mec.biz.service.StudentRepairService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.exception.BizException;

@RequestMapping("repair")
@Api(tags = "学生维修服务")
@RestController
public class RepairController extends BaseController {

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

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

    @ApiOperation("学员扫码支付")
    @PostMapping(value = "/studentPaymentGoodsOrder")
    public HttpResponseResult studentPaymentGoodsOrder(Integer goodsSellId) throws Exception {
        Map map = studentRepairService.studentPaymentGoodsOrder(goodsSellId);
        if(map.containsKey("tradeState")){
            return failed(HttpStatus.CREATED, "恭喜您,购买成功!");
        }
        return succeed(map);
    }



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

    @ApiOperation("获取维修记录详情")
    @GetMapping(value = "/getRepairInfo")
    public HttpResponseResult getRepairInfo(Integer id,Integer studentId) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if ((sysUser == null || sysUser.getId() ==null) && studentId == null) {
            return failed("用户信息获取失败");
        }
        if(sysUser.getId() != null){
            studentId = sysUser.getId();
        }
        StudentRepair repairInfo = studentRepairService.getRepairInfo(id);
        if (!repairInfo.getStudentId().equals(studentId)) {
            return failed("您的维修记录不存在");
        }
        return succeed(repairInfo);
    }

    @ApiOperation("支付维修单")
    @PostMapping(value = "/payRepair")
    public HttpResponseResult payRepair(StudentRepair repairInfo) throws Exception {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(studentRepairService.payRepair(repairInfo));
    }

    @ApiOperation("获取维修技师信息")
    @PostMapping(value = "/getRepairer")
    public HttpResponseResult getRepairer() {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(studentRepairService.getStudentRepairer(sysUser.getId(),sysUser.getOrganId()));
    }

    @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.setStudentId(sysUser.getId());
        return succeed(studentGoodsSellService.queryStudentGoodsOrders(queryInfo));
    }

    @ApiOperation(value = "获取学员商品订单")
    @GetMapping("/getStudentGoodsOrder")
    public Object getStudentGoodsOrder(Integer goodsSellId) {
        return succeed(studentGoodsSellService.get(goodsSellId));
    }

    @ApiOperation(value = "确认收货")
    @PostMapping("/affirmReceive")
    public Object affirmReceive(String orderNo) {
        studentGoodsSellService.affirmReceive(orderNo);
        return succeed();
    }
}
