package com.miaxis.pc.controller.wx;

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.utils.SecurityUtils;
import com.miaxis.system.service.ISysRoleService;
import com.miaxis.system.service.ISysUserService;
import com.miaxis.wx.dto.QuerySchoolOrderListDTO;
import com.miaxis.wx.service.IRefundRecordService;
import com.miaxis.wx.service.IWxOrderService;
import com.miaxis.wx.vo.WxSchoolOrder;
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.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 */
@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);
//    }






    @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('wx:order:list')")
    public ResponsePageInfo<WxSchoolOrder> schoolList(@ModelAttribute QuerySchoolOrderListDTO dto){
        startPage();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Set<String> permissions = SecurityUtils.getLoginUser().getPermissions();
        dto.setSchoolName(user.getUserId()==1l||permissions.contains("schoolOrder:index:search")? dto.getSchoolName(): user.getNickName());
        List<WxSchoolOrder> list = wxOrderService.QuerySchoolOrderList(dto);
        ResponsePageInfo<WxSchoolOrder> pageData = toResponsePageInfo(list);
        Map mapData = new HashMap<>();
        mapData.put("totalPay",wxOrderService.getTotalPay(dto));
        pageData.setOtherData(mapData);
        return pageData;
    }


    /**
     * 查询驾校列表
     */
    @PreAuthorize("@ss.hasPermi('wx:order:list')")
    @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);
    }














}
