package com.zcloud.controller.positioning;

import com.alibaba.fastjson.JSONObject;
import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.service.system.UsersService;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.PLSUtil;
import com.zcloud.util.ReturnMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 说明：用户定位卡管理
 * 作者：wangxuan
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/deploy/card")
public class UserCardController extends BaseController {


    @Autowired
    private UsersService usersService;

    /**
     * @Description: 人员卡管理
     * @Author: dearLin
     * @Date: 2024/1/26/026 16:43
     * @Param: [] []
     * @Return: com.zcloud.util.ReturnMap
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public ReturnMap list(Page page) throws Exception {
        PageData pageData = this.getPageData();
        ReturnMap returnMap = new ReturnMap();
        page.setPd(pageData);
        JSONObject body = PLSUtil.getUserCardDataList(
                Jurisdiction.getCORPINFO_ID(), Integer.parseInt(pageData.getString("currentPage")), Integer.parseInt(pageData.getString("showCount")), pageData.getString("KEYWORDS"));
        if (body != null) {
            List<JSONObject> list = body.getJSONArray("rows").toJavaList(JSONObject.class);
            returnMap.put("varList", list);
            page.setTotalResult(body.getIntValue("total"));
            returnMap.put("page", page);
        }
        return returnMap;
    }

    /**
     * @Description: 修改卡信息
     * @Author: dearLin
     * @Date: 2024/1/26/026 16:43
     * @Param: [] []
     * @Return: com.zcloud.util.ReturnMap
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public ReturnMap edit() throws Exception {
        PageData pageData = this.getPageData();
        ReturnMap returnMap = new ReturnMap();
        JSONObject body = PLSUtil.GeneralPut("/deploy/card", pageData, Jurisdiction.getCORPINFO_ID());
        if (body != null) {
            returnMap.putAll(body);
            if (!"200".equals(body.getString("code"))) return ReturnMap.error(body.getString("msg"));
            usersService.editUserCardNoByPid(pageData);
        }
        return returnMap;
    }

    /**
     * @Description: 可绑卡人信息
     * @Author: dearLin
     * @Date: 2024/1/26/026 16:43
     * @Param: [] []
     * @Return: com.zcloud.util.ReturnMap
     */
    @RequestMapping(value = "/thePersonWhoDidNotBindTheCard")
    @ResponseBody
    public ReturnMap thePersonWhoDidNotBindTheCard() throws Exception {
        ReturnMap returnMap = new ReturnMap();
        JSONObject body = PLSUtil.GetGeneral("/deploy/card/thePersonWhoDidNotBindTheCard", Jurisdiction.getCORPINFO_ID());
        if (body != null) {
            returnMap.putAll(body);
        }
        return returnMap;
    }


    /**
     * @Description: 查找信息
     * @Author: dearLin
     * @Date: 2024/1/26/026 16:43
     * @Param: [] []
     * @Return: com.zcloud.util.ReturnMap
     */
    @RequestMapping(value = "/goEdit")
    @ResponseBody
    public ReturnMap goEdit() throws Exception {
        PageData pageData = getPageData();
        ReturnMap returnMap = new ReturnMap();
        JSONObject body = PLSUtil.GeneralGetById("/deploy/card/feign-client/", Integer.parseInt(pageData.getString("cardNo")), Jurisdiction.getCORPINFO_ID());
        if (body != null) {
            returnMap.putAll(body);
        }
        return returnMap;
    }

    /**
     * @Description: 删除卡
     * @Author: dearLin
     * @Date: 2024/1/26/026 16:43
     * @Param: [] []
     * @Return: com.zcloud.util.ReturnMap
     */
    @RequestMapping(value = "/remove")
    @ResponseBody
    public ReturnMap remove() throws Exception {
        PageData pageData = getPageData();
        ReturnMap returnMap = new ReturnMap();
        JSONObject body = PLSUtil.GeneralDelete("/deploy/card/", pageData.getString("DATA_IDS"), Jurisdiction.getCORPINFO_ID());
        if (body != null) {
            if (!"200".equals(body.getString("code"))) return ReturnMap.error(body.getString("msg"));

            returnMap.putAll(body);
        }
        return returnMap;
    }

    /**
     * @Description: 批量解绑
     * @Author: dearLin
     * @Date: 2024/1/26/026 16:43
     * @Param: [] []
     * @Return: com.zcloud.util.ReturnMap
     */
    @RequestMapping(value = "/batchUnbinding")
    @ResponseBody
    public ReturnMap removeBatch() throws Exception {
        PageData pageData = getPageData();
        ReturnMap returnMap = new ReturnMap();
        JSONObject body = PLSUtil.GeneralDelete("/deploy/card/batchUnbinding/", pageData.getString("DATA_IDS"), Jurisdiction.getCORPINFO_ID());
        if (body != null) {
            if (!"200".equals(body.getString("code"))) return ReturnMap.error(body.getString("msg"));
            returnMap.putAll(body);
            // 人员表里给 对应卡号得人 得 卡移除掉
            String[] dataIds = pageData.getString("DATA_IDS").split(",");
            pageData.put("USER_IDS", dataIds);
            pageData.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
            usersService.removeCardNoUser(pageData);
        }
        return returnMap;
    }

    /**
     * @Description: 批量制卡
     * @Author: dearLin
     * @Date: 2024/2/21/021 15:33
     * @Param: [] []
     * @Return: com.zcloud.util.ReturnMap
     */
    @RequestMapping(value = "/addInBulk")
    @ResponseBody
    public ReturnMap addInBulk() throws Exception {
        PageData pageData = this.getPageData();
        ReturnMap returnMap = new ReturnMap();
        JSONObject body = PLSUtil.GeneralPost("/deploy/card/addInBulk", pageData, Jurisdiction.getCORPINFO_ID());
        if (body != null) {
            returnMap.putAll(body);
        }
        return returnMap;
    }

}
