package com.winsun.controller;

import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.bean.Order;
import com.winsun.bean.School;
import com.winsun.bean.SysUser;
import com.winsun.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 首页同用接口
 *
 * @Author: chancy
 * @Date: 2020/3/9 10:16
 */
@Slf4j
@RestController
@RequestMapping("/choose")
public class ChooseController {

    private static AppMapper appMapper;

    private static PackageMapper packageMapper;

    private static SchoolMapper schoolMapper;

    private static OrderMapper orderMapper;

    private static SysUserMapper userMapper;

    @Autowired
    @Qualifier("redisStringTemplate")
    private RedisTemplate redisTemplate;

    @Autowired
    public ChooseController(AppMapper appMapper, PackageMapper packageMapper,SchoolMapper schoolMapper, OrderMapper orderMapper
            , SysUserMapper userMapper) {
        ChooseController.appMapper = appMapper;
        ChooseController.packageMapper = packageMapper;
        ChooseController.schoolMapper = schoolMapper;
        ChooseController.orderMapper = orderMapper;
        ChooseController.userMapper = userMapper;
    }


    /**
     * 查询订单详情
     */
    @RequestMapping(value = "selectOrderInfo", method = RequestMethod.POST)
    public ResponseData<Order> selectOrderInfo(@RequestParam("orderId") String orderId) {
        Order order = orderMapper.selectById(orderId);
        order.setIdCard("");
        order.setGiftAccount("");
        order.setGiftPassword("");
        return ResponseData.success(order);
    }

    /**
     * 查询用户详情
     */
    @RequestMapping(value = "selectUserInfo", method = RequestMethod.POST)
    public ResponseData<Map<String, Object>> selectUserInfo(@RequestParam("partner") String partner) {
        SysUser sysUser = userMapper.selectById(partner);
        if(sysUser==null){
            return ResponseData.error("没有查到用户信息！");
        }
        String schoolId = appMapper.selectSchoolId(partner);
        String schoolName = appMapper.selectSchoolById(schoolId);
        Map<String, Object> map = new HashMap<>();
        map.put("schoolName", schoolName);
        map.put("substName", sysUser.getSubstName());
        map.put("name", sysUser.getName());
        map.put("id", sysUser.getId());
        map.put("account", sysUser.getAccount());
        map.put("phone", sysUser.getPhone());
        return ResponseData.success(map);
    }

    /**
     * 查询订单历史状态
     */
    @RequestMapping(value = "selectOrderHis", method = RequestMethod.POST)
    public ResponseData<List<Map<String, Object>>> selectOrderHis(@RequestParam("orderId") String orderId) {
        List<Map<String, Object>> list = appMapper.selectOrderHis(orderId);
        return ResponseData.success(list);
    }

    /**
     * 查询学校信息
     */

    @RequestMapping(value = "selectSchoolInfo", method = RequestMethod.POST)
    public ResponseData<School> selectSchoolInfo(@RequestParam("partner") String partner) {
        String schoolId = appMapper.selectSchoolId(partner);
        School school = schoolMapper.selectById(schoolId);
        return ResponseData.success(school);
    }

}
