package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.HighTyAgent;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.HighTyAgentService;
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 javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/tyAgent")
@Api(value = "订单接口")
public class HighTyAgentController {

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

    @Resource
    private HighTyAgentService tyAgentService;

    @Resource
    private UserCenter userCenter;

    @RequestMapping(value = "/addAgent", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "增加代理商")
    public ResponseData addAgent(@RequestBody JSONObject body, HttpServletRequest request) {
        try {
            UserInfoModel userModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }
            if (userModel.getBsOrganization() == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMPETENCE_INSUFFICIENT, "");
            }

            if (StringUtils.isBlank(body.getString("agentName"))
                    || StringUtils.isBlank(body.getString("agentUser"))
                    || StringUtils.isBlank(body.getString("agentPhone"))
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQUEST_ERROR, "");
            }

            HighTyAgent tyAgent = new HighTyAgent();
            tyAgent.setOrgId(userModel.getBsOrganization().getId());
            tyAgent.setAgentName(body.getString("agentName"));
            tyAgent.setAgentUser(body.getString("agentUser"));
            tyAgent.setAgentPhone(body.getString("agentPhone"));
            tyAgent.setAgentAddress(body.getString("agentAddress"));
            tyAgentService.addAgent(tyAgent);

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

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

    @RequestMapping(value = "/updateAgent", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改代理商")
    public ResponseData updateAgent(@RequestBody JSONObject body, HttpServletRequest request) {
        try {

            if (body.getLong("id") == null
                    || StringUtils.isBlank(body.getString("agentName"))
                    || StringUtils.isBlank(body.getString("agentUser"))
                    || StringUtils.isBlank(body.getString("agentPhone"))
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQUEST_ERROR, "");
            }

            // 查询代理商
            HighTyAgent tyAgent = tyAgentService.getDetailById(body.getLong("id"));
            if (tyAgent == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到代理商信息");
            }
            tyAgent.setAgentName(body.getString("agentName"));
            tyAgent.setAgentUser(body.getString("agentUser"));
            tyAgent.setAgentPhone(body.getString("agentPhone"));
            tyAgentService.updateAgent(tyAgent);

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

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

    @RequestMapping(value = "/delAgent", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除代理商")
    public ResponseData delAgent(@RequestBody JSONObject body, HttpServletRequest request) {
        try {
            if (StringUtils.isBlank(body.getString("agentKey"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQUEST_ERROR, "");
            }
            tyAgentService.delAgent(body.getString("agentKey"));

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

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

    @RequestMapping(value = "/agentPwdReset", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "代理商密码重置")
    public ResponseData agentPwdReset(@RequestBody JSONObject body, HttpServletRequest request) {
        try {
            if (StringUtils.isBlank(body.getString("agentKey"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQUEST_ERROR, "");
            }
            tyAgentService.agentPwdReset(body.getString("agentKey"));

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

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

    @RequestMapping(value = "/getDetailByKey", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询代理商详情")
    public ResponseData getDetailByKey(@RequestParam(name = "key", required = true) String key) {
        try {

            return ResponseMsgUtil.success(tyAgentService.getDetailByKey(key));

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

    @RequestMapping(value = "/getAgentList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询代理商列表")
    public ResponseData getAgentList(@RequestParam(name = "orgId", required = false) Long orgId,
                                     @RequestParam(name = "agentKey", required = false) String agentKey,
                                     @RequestParam(name = "agentName", required = false) String agentName,
                                     @RequestParam(name = "agentUser", required = false) String agentUser,
                                     @RequestParam(name = "agentPhone", required = false) String agentPhone,
                                     @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                     @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {

            Map<String, Object> param = new HashMap<>();
            if (orgId == null) {
                UserInfoModel userModel = userCenter.getSessionModel(UserInfoModel.class);
                if (userModel == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
                }
                if (userModel.getBsOrganization() == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMPETENCE_INSUFFICIENT, "");
                }
                param.put("orgId", userModel.getBsOrganization().getId());
            } else {
                param.put("orgId", orgId);
            }
            param.put("agentKey", agentKey);
            param.put("agentName", agentName);
            param.put("agentUser", agentUser);
            param.put("agentPhone", agentPhone);

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

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




}
