package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.service.agent.BsAgentMerService;
import com.hfkj.sysenum.SecUserObjectTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@Controller
@RequestMapping(value = "/agentMer")
@Api(value = "代理商商户")
public class BsAgentMerController {

    private static Logger log = LoggerFactory.getLogger(BsAgentMerController.class);

    @Resource
    private BsAgentMerService agentMerService;
    @Resource
    private UserCenter userCenter;
    @RequestMapping(value = "/assignAgent", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分配代理商")
    public ResponseData assignAgent(@RequestBody JSONObject body) {
        try {
            if (body.getLong("agentId") == null
                    || body.getJSONArray("merNoList") == null
                    || body.getJSONArray("merNoList").size() == 0
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            List<String> merNoList = new ArrayList<>();
            for (Object obj : body.getJSONArray("merNoList")) {
                JSONObject objData = (JSONObject)obj;
                if (StringUtils.isBlank(objData.getString("merNo"))) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
                merNoList.add(objData.getString("merNo"));
            }
            agentMerService.assignAgent(body.getLong("agentId"), merNoList);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("BsAgentMerController --> assignAgent() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/unbindAgent", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "解绑代理商")
    public ResponseData unbindAgent(@RequestBody JSONObject body) {
        try {
            if (body.getLong("agentId") == null
                    || body.getJSONArray("merNoList") == null
                    || body.getJSONArray("merNoList").isEmpty()
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            List<String> merNoList = new ArrayList<>();
            for (Object obj : body.getJSONArray("merNoList")) {
                JSONObject objData = (JSONObject)obj;
                if (StringUtils.isBlank(objData.getString("merNo"))) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
                merNoList.add(objData.getString("merNo"));
            }
            agentMerService.unbindAgent(body.getLong("agentId"), merNoList);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("HighTyAgentController --> assignOrg() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }
    @RequestMapping(value = "/assignStaff", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分配工作人员")
    public ResponseData assignStaff(@RequestBody JSONObject body) {
        try {
            SecUserSessionObject userSession = userCenter.getSessionModel(SecUserSessionObject.class);
            if (!userSession.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type4.getCode())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS, "");
            }

            if (body.getLong("staffId") == null
                    || body.getJSONArray("merNoList") == null
                    || body.getJSONArray("merNoList").isEmpty()
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            List<String> merNoList = new ArrayList<>();
            for (Object obj : body.getJSONArray("merNoList")) {
                JSONObject objData = (JSONObject)obj;
                if (StringUtils.isBlank(objData.getString("merNo"))) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
                merNoList.add(objData.getString("merNo"));
            }

            agentMerService.assignStaff(body.getLong("staffId"), merNoList);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }
    @RequestMapping(value = "/unbindStaff", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "解绑业务员")
    public ResponseData unbindStaff(@RequestBody JSONObject body) {
        try {
            SecUserSessionObject userSession = userCenter.getSessionModel(SecUserSessionObject.class);
            if (!userSession.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type4.getCode())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS, "");
            }

            if (body.getLong("staffId") == null
                    || body.getJSONArray("merNoList") == null
                    || body.getJSONArray("merNoList").isEmpty()
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            List<String> merNoList = new ArrayList<>();
            for (Object obj : body.getJSONArray("merNoList")) {
                JSONObject objData = (JSONObject)obj;
                if (StringUtils.isBlank(objData.getString("merNo"))) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
                merNoList.add(objData.getString("merNo"));
            }

            agentMerService.unbindStaff(body.getLong("staffId"), merNoList);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }
    @RequestMapping(value = "/getAllotList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询分配列表")
    public ResponseData getAllotList(@RequestParam(name = "agentId", required = false) Long agentId,
                                     @RequestParam(name = "isAgentAllot", required = false) Boolean isAgentAllot,
                                     @RequestParam(name = "agentStaffId", required = false) Long agentStaffId,
                                     @RequestParam(name = "isAgentStaffAllot", required = false) Boolean isAgentStaffAllot,
                                     @RequestParam(name = "provinceCode", required = false) String provinceCode,
                                     @RequestParam(name = "merSourceType", required = false) Long merSourceType,
                                     @RequestParam(name = "merNo", required = false) String merNo,
                                     @RequestParam(name = "merName", required = false) String merName,
                                     @RequestParam(name = "status", required = false) Integer status,
                                     @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                     @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            SecUserSessionObject userSession = userCenter.getSessionModel(SecUserSessionObject.class);
            Map<String, Object> param = new HashMap<>();
            param.put("provinceCode", StringUtils.isNotBlank(provinceCode)?provinceCode:null);
            param.put("merSourceType", merSourceType);
            param.put("merNo", StringUtils.isNotBlank(merNo)?merNo:null);
            param.put("merName", StringUtils.isNotBlank(merName)?merName:null);
            param.put("status", status);

            if (userSession.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type1.getCode())) {
                param.put("agentId", agentId);
                param.put("isAgentAllot", isAgentAllot);
                param.put("agentStaffId", agentStaffId);
                param.put("isAgentStaffAllot", isAgentStaffAllot);
            } else if (userSession.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type4.getCode())) {
                param.put("agentId", userSession.getAccount().getObjectId());
                param.put("isAgentAllot", true);
                param.put("agentStaffId", agentStaffId);
                param.put("isAgentStaffAllot", isAgentStaffAllot);
            } else if (userSession.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type5.getCode())) {
                param.put("agentStaffId", userSession.getAccount().getObjectId());
                param.put("isAgentStaffAllot", true);
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS, "");
            }

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(agentMerService.getAllotList(param)));

        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }
}
