package com.winsun.controller;

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.Package;
import com.winsun.mapper.OrderMapper;
import com.winsun.mapper.PackageMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 移动端订单查询
 *
 * @date 2021-03-17 11:30
 */
@RestController
@RequestMapping("/supervisor") //
public class SupervisorController {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private PackageMapper packageMapper;

    /**
     * 督导订单详情查询
     *
     * @param orderId
     * @return
     */
    @PostMapping("selectOrderInfo")
    public ResponseData<Map<String, Object>> selectOrderInfo(@RequestParam("orderId") String orderId) {
       /* ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }*/

        if (StringUtils.isNotBlank(orderId)) {
            ResponseData<String> verification = verification(orderId);
            if (!verification.isSuccess()) {
                return ResponseData.error("订单不存在或异常！");
            }
        } else {
            return ResponseData.error("页面已失效，请重新登录！");
        }
        Wrapper<Order> wrapper = new EntityWrapper<>();
        wrapper.eq("id", orderId);
        List<Map<String, Object>> list = orderMapper.selectMaps(wrapper);
        Map<String, Object> map = list.get(0);
        if (map.size() == 0) {
            return ResponseData.error("当前订单不存在！");
        }
        if (map.get("packageId") != null && map.get("kapin") != "融合办理") {
            Package aPackage = packageMapper.selectById(Integer.parseInt(map.get("packageId").toString()));
            map.put("packageName", aPackage.getPackageName());
            map.put("advImg", aPackage.getAdvImg());
            map.put("explains", aPackage.getExplains());
        }

        //测试用
        if (StringUtils.isBlank(map.get("idCard").toString())) {
            map.put("idCard", "000000000000000000".substring(0, 14) + "****");
        } else {
            if (map.get("idCard").toString().length() == 18) {
                map.put("idCard", map.get("idCard").toString().substring(0, 14) + "****");
            } else {
                map.put("idCard", map.get("idCard").toString().substring(0, 11) + "****");
            }
        }

        //查询历史状态
        List<Map<String, Object>> orderHis = orderMapper.selectOrderHis(orderId);
        map.put("orderHis", orderHis);
        return ResponseData.success(map);
    }

    /**
     * 验证订单
     *
     * @param orderId
     * @return
     */
    public ResponseData<String> verification(String orderId) {
        Wrapper wrapper = new EntityWrapper<>();
        wrapper.eq("id", orderId);
        List list = orderMapper.selectList(wrapper);
        if (list.size() == 1) {
            return ResponseData.success("ok");
        } else if (list.size() == 0) {
            return ResponseData.error("订单不存在！");
        } else {
            return ResponseData.error("订单异常！");
        }
    }
}
