package com.winsun.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.winsun.auth.core.base.controller.BaseController;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.auth.core.shiro.ShiroUser;
import com.winsun.bean.Order;
import com.winsun.bean.Package;
import com.winsun.mapper.OrderMapper;
import com.winsun.mapper.PackageMapper;
import com.winsun.utils.MyBatisPlusUpdateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @Author: 张宸
 * @Date: 2020年3月11日15:12:46
 */
@Slf4j
@RestController
@RequestMapping("/supervisor")
public class supervisorController extends BaseController {

    private static PackageMapper packageMapper;

    private static OrderMapper orderMapper;

    @Autowired
    public supervisorController(PackageMapper packageMapper, OrderMapper orderMapper) {
        supervisorController.packageMapper = packageMapper;
        supervisorController.orderMapper = orderMapper;
    }

    /**
     * 督导订单详情查询
     *
     * @param orderId
     * @return
     */
    @RequestMapping(value = "selectOrderInfo", method = RequestMethod.POST)
    public ResponseData<Map<String, Object>> selectOrderInfo(String orderId) {
       /* ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            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("当前订单不存在！");
        }
        Package aPackage = packageMapper.selectById(Integer.parseInt(map.get("packageId").toString()));
        map.put("packageName", aPackage.getPackageName());
        map.put("advImg", aPackage.getAdvImg());
        //测试用
        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) + "****");
            }
        }
        map.put("explains", aPackage.getExplains());

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

    /**
     * 督导订单置为异常单
     *
     * @param orderId
     * @return
     */
    @RequestMapping(value = "updateOrderState", method = RequestMethod.POST)
    public ResponseData<String> updateOrderState(String orderId) {
        /* ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }*/
        Wrapper wrapper = new EntityWrapper<>();
        wrapper.eq("id",orderId);
        Map<String, Object> dataMapping = new HashMap<>();
        dataMapping.put("order_status", "异常单");
        Integer integer = orderMapper.updateForSet(MyBatisPlusUpdateUtils.toUpdateSet(dataMapping), wrapper);
        if(integer == 0){
            return  ResponseData.error("置为异常单失败！");
        }else {
            //orderMapper.inserOrderHis(orderId,"已置为异常单",new Date(),user.getName());
            orderMapper.inserOrderHis(orderId,"已置为异常单",new Date(),"测试");
            return ResponseData.success("置为异常单成功！");
        }
    }

}
