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.utils.ResponseMsgUtil;
import com.hfkj.entity.BsAgent;
import com.hfkj.entity.SecUser;
import com.hfkj.model.ResponseData;
import com.hfkj.service.agent.BsAgentService;
import com.hfkj.service.sec.SecUserService;
import com.hfkj.sysenum.SecUserObjectTypeEnum;
import com.hfkj.sysenum.agent.AgentTypeEnum;
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.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/agent")
@Api(value = "代理商管理")
public class BsAgentController {

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

    @Resource
    private BsAgentService agentService;
    @Resource
    private SecUserService secUserService;

    @RequestMapping(value = "/createAgent", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "创建代理商")
    public ResponseData createAgent(@RequestBody JSONObject body) {
        try {
            if (body == null
                || body.getInteger("type") == null
                || StringUtils.isBlank(body.getString("loginName"))
                || StringUtils.isBlank(body.getString("name"))
                || StringUtils.isBlank(body.getString("contactsName"))
                || StringUtils.isBlank(body.getString("contactsTelephone"))
            ) {
                log.error("BsAgentController --> createAgent() error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 代理商类型
            AgentTypeEnum agentType = AgentTypeEnum.getDataByType(body.getInteger("type"));
            if (agentType == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的代理类型");
            }
            BsAgent agent = new BsAgent();
            agent.setType(body.getInteger("type"));
            agent.setName(body.getString("name"));
            agent.setContactsName(body.getString("contactsName"));
            agent.setContactsTelephone(body.getString("contactsTelephone"));

            agentService.createAgent(body.getString("loginName"), agent);
            return ResponseMsgUtil.success("创建成功");
        } catch (Exception e) {
            log.error("BsAgentController --> createAgent() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/updateAgent", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改代理商")
    public ResponseData updateAgent(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || StringUtils.isBlank(body.getString("agentNo"))
                    || StringUtils.isBlank(body.getString("name"))
                    || StringUtils.isBlank(body.getString("contactsName"))
                    || StringUtils.isBlank(body.getString("contactsTelephone"))
            ) {
                log.error("BsAgentController --> updateAgent() error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            // 查询代理商
            BsAgent agent = agentService.getAgentByAgentNo(body.getString("agentNo"));
            if (agent == null) {
                log.error("BsAgentController --> updateAgent() error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的代理商");
            }
            agent.setName(body.getString("name"));
            agent.setContactsName(body.getString("contactsName"));
            agent.setContactsTelephone(body.getString("contactsTelephone"));
            agentService.updateAgent(agent);
            
            return ResponseMsgUtil.success("修改成功");
        } catch (Exception e) {
            log.error("BsAgentController --> updateAgent() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/disable", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "禁用代理商")
    public ResponseData disable(@RequestBody JSONObject body) {
        try {
            if (body == null || StringUtils.isBlank(body.getString("agentNo"))) {
                log.error("BsAgentController --> disable() error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 查询代理商
            BsAgent agent = agentService.getAgentByAgentNo(body.getString("agentNo"));
            if (agent == null) {
                log.error("BsAgentController --> disable() error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的代理商");
            }
            agentService.disable(body.getString("agentNo"));

            return ResponseMsgUtil.success("操作成功");
        } catch (Exception e) {
            log.error("BsAgentController --> disable() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "启用代理商")
    public ResponseData enable(@RequestBody JSONObject body) {
        try {
            if (body == null || StringUtils.isBlank(body.getString("agentNo"))) {
                log.error("BsAgentController --> enable() error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            // 查询代理商
            BsAgent agent = agentService.getAgentByAgentNo(body.getString("agentNo"));
            if (agent == null) {
                log.error("BsAgentController --> enable() error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的代理商");
            }
            agentService.enable(body.getString("agentNo"));

            return ResponseMsgUtil.success("修改成功");
        } catch (Exception e) {
            log.error("BsAgentController --> enable() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }


    @RequestMapping(value = "/queryAgentDetail", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询代理商详情")
    public ResponseData queryAgentDetail(@RequestParam(name = "agentNo", required = true) String agentNo) {
        try {
            // 查询代理商
            BsAgent agent = agentService.getAgentByAgentNo(agentNo);
            if (agent == null) {
                log.error("BsAgentController --> queryAgentDetail() error!", "未找到代理商");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到代理商");
            }
            Map<String,Object> param = new HashMap<>();
            param.put("agent", agent);

            SecUser secUser = secUserService.getDetail(SecUserObjectTypeEnum.type4, agent.getId());
            if (secUser == null) {
                log.error("BsAgentController --> queryAgentDetail() error!", "未找到代理商登录账户");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到代理商登录账户");
            }
            secUser.setPassword(null);
            param.put("account", secUser);

            return ResponseMsgUtil.success(param);
        } catch (Exception e) {
            log.error("BsAgentController --> queryAgentDetail() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/queryAgentList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询代理商列表")
    public ResponseData queryAgentList(@RequestParam(name = "type", required = false) String type,
                                       @RequestParam(name = "agentNo", required = false) String agentNo,
                                       @RequestParam(name = "agentName", required = false) String agentName,
                                       @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                       @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("type", type);
            param.put("agentNo", agentNo);
            param.put("agentName", agentName);

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(agentService.getAgentList(param)));
        } catch (Exception e) {
            log.error("BsAgentController --> queryAgentList() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
