package com.miaxis.pc.controller.wx;

import com.miaxis.common.annotation.Log;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.core.domain.entity.SysRole;
import com.miaxis.common.core.domain.entity.SysUser;
import com.miaxis.common.core.domain.vo.AgentVO;
import com.miaxis.common.core.page.ResponsePageInfo;
import com.miaxis.common.enums.BusinessTypeEnum;
import com.miaxis.common.enums.OrderStatusEnum;
import com.miaxis.common.exception.CustomException;
import com.miaxis.common.utils.SecurityUtils;
import com.miaxis.common.utils.StringUtils;
import com.miaxis.system.service.ISysRoleService;
import com.miaxis.system.service.ISysUserService;
import com.miaxis.wx.domain.WxOrder;
import com.miaxis.wx.dto.PcWxOrderListDTO;
import com.miaxis.wx.dto.QuerySchoolOrderListDTO;
import com.miaxis.wx.dto.WxNotifyReturnDTO;
import com.miaxis.wx.service.IRefundRecordService;
import com.miaxis.wx.service.IWxOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.miaxis.common.utils.OrderCodeFactory.getOrderCode;

/**
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/activities/wx")
@Api(tags = {"【pc-订单】"})
@Slf4j
class PcWxController extends BaseController {



    @Autowired
    private IWxOrderService wxOrderService;

    @Autowired
    private IRefundRecordService refundRecordService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;




    @GetMapping("/order/list")
    @ApiOperation("查询订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码" ,dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize",value = "每页数据量" , dataType = "int", paramType = "query", required = false),
    })
    @PreAuthorize("@ss.hasPermi('activities:order:list')")
    public ResponsePageInfo<WxOrder> list(@ModelAttribute PcWxOrderListDTO pcWxOrderListDTO){
        startPage();
        List<WxOrder> list = wxOrderService.selectPcOrderList(pcWxOrderListDTO);
        return toResponsePageInfo(list);
    }



    /**
     * 订单退款
     */
    @PreAuthorize("@ss.hasPermi('activities:order:refund')")
    @Log(title = "订单退款", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    @ApiOperation("订单退款")
    public Response refund(String outTradeNo) throws Exception {
        if (StringUtils.isEmpty(outTradeNo)){
            throw new CustomException("订单号必传");
        }
        WxOrder wxOrder = wxOrderService.getByOutTradeNo(outTradeNo);
        String refundCode = getOrderCode(null);
        refundRecordService.refund(wxOrder, refundCode, "学车会员退款");
        return Response.success();
    }


    @GetMapping("/order/school/list")
    @ApiOperation("查询驾校订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码" ,dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize",value = "每页数据量" , dataType = "int", paramType = "query", required = false),
    })
    @PreAuthorize("@ss.hasPermi('activities:order:schoolList')")
    public ResponsePageInfo<WxOrder> schoolList(@ModelAttribute QuerySchoolOrderListDTO dto){
        startPage();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        dto.setReferralUserId(user.getUserId()==1l? dto.getReferralUserId(): user.getUserId());
        List<WxOrder> list = wxOrderService.QuerySchoolOrderList(dto);
        return toResponsePageInfo(list);
    }


    /**
     * 查询驾校列表
     */
    @PreAuthorize("@ss.hasPermi('activities:order:schoolList')")
    @GetMapping("/schoolList")
    @ApiOperation("查询驾校列表")
    public Response<List<AgentVO>> agentList(String schoolName){
        SysRole sysRole = new SysRole();
        sysRole.setRoleKey("school");
        List<SysRole> sysRoles = roleService.selectRoleList(sysRole);
        Long roleId = sysRoles.get(0).getRoleId();
        List<AgentVO> sysUsersList =  userService.getAgentList(roleId,schoolName);
        return Response.success(sysUsersList);
    }














}
