package com.winsun.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
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 com.winsun.utils.MyBatisPlusUpdateUtils;
import com.winsun.utils.XbkUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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.Date;
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);
        if("待活体".equals(order.getOrderStatus()) || "待识别".equals(order.getOrderStatus())){
            general(orderId);
        }
        order.setIdCard("");
        order.setGiftAccount("");
        order.setGiftPassword("");
        order.setNetPassword("");
        if(StringUtils.isNotBlank(order.getNetNumber())){
            String[] split = order.getNetNumber().split("@");
            order.setNetNumber(order.getNetNumber().substring(0,1)+"****"+split[1]);
        }
        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);
    }

    public static void general(String orderId) {
        Wrapper<Order> wrapper = new EntityWrapper();
        wrapper.eq("id", orderId);
        List<Order> orders = orderMapper.selectList(wrapper);
        orders.forEach(order -> {
            try {
                Map<String, Object> dataMapping = new HashMap<>();
                JSONObject jsonObject = XbkUtil.orderInfoJT0006(order.getXbOrderId());
                String dataArray = jsonObject.getString("dataArray");
                Map<String,Object> Map = JSON.parseObject(dataArray, HashMap.class);
                Wrapper<Order> wrapper1 = new EntityWrapper();
                wrapper1.eq("xb_order_id",Map.get("orderCode"));
                wrapper1.eq("id",order.getId());
                if(Map.get("orderState").toString().equals("3") || Map.get("orderState").toString().equals("13") ||
                        Map.get("orderState").toString().equals("8") || Map.get("orderState").toString().equals("22") ||
                        Map.get("orderState").toString().equals("21") ){
                    dataMapping.put("order_status", "待处理");
                }
                if(Map.get("orderState").toString().equals("10") || Map.get("orderState").toString().equals("11")){
                    dataMapping.put("order_status", "异常单");
                    dataMapping.put("success_time", new Date());
                }
                orderMapper.updateForSet(MyBatisPlusUpdateUtils.toUpdateSet(dataMapping), wrapper1);
                log.info(order.getXbOrderId()+"订单已更新");
            } catch (Exception e) {
                log.error(order.getXbOrderId()+"订单更新失败",e.getMessage());
            }
        });
    }
}
