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.entity.BsAgentStaff;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.service.agent.BsAgentStaffService;
import com.hfkj.service.sec.SecUserService;
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 javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/agentStaff")
@Api(value = "代理商管理")
public class BsAgentStaffController {
    private static Logger log = LoggerFactory.getLogger(BsAgentStaffController.class);
    @Resource
    private BsAgentStaffService agentStaffService;
    @Resource
    private SecUserService secUserService;
    @Resource
    private UserCenter userCenter;

    @RequestMapping(value = "/addStaff", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "增加业务员")
    public ResponseData addStaff(@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 (StringUtils.isBlank(body.getString("name"))
                    || StringUtils.isBlank(body.getString("telephone"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            BsAgentStaff staff = new BsAgentStaff();
            staff.setAgentId(userSession.getAccount().getObjectId());
            staff.setName(body.getString("name"));
            staff.setTelephone(body.getString("telephone"));
            agentStaffService.addStaff(staff);

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

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

    @RequestMapping(value = "/updateStaff", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改业务员")
    public ResponseData updateStaff(@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("id") == null
                    || StringUtils.isBlank(body.getString("name"))
                    || StringUtils.isBlank(body.getString("telephone"))
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            // 查询代理商
            BsAgentStaff staff = agentStaffService.getStaffDetailById(body.getLong("id"));
            if (staff == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到代理商信息");
            }
            staff.setName(body.getString("name"));
            staff.setTelephone(body.getString("telephone"));
            agentStaffService.updateStaff(staff);

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

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

    @RequestMapping(value = "/delStaff", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除业务员")
    public ResponseData delStaff(@RequestBody JSONObject body) {
        try {
            if (body.getLong("id") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            agentStaffService.delStaff(body.getLong("id"));

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

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

    @RequestMapping(value = "/staffPwdReset", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "业务员密码重置")
    public ResponseData staffPwdReset(@RequestBody JSONObject body, HttpServletRequest request) {
        try {
            if (body.getLong("id") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            agentStaffService.staffPwdReset(body.getLong("id"));

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

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

    @RequestMapping(value = "/getStaffDetail", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询工作人员详情")
    public ResponseData getStaffDetail(@RequestParam(name = "id", required = true) Long id) {
        try {
            BsAgentStaff staff = agentStaffService.getStaffDetailById(id);
            if (staff == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的工作人员");
            }
            Map<String,Object> map = new HashMap<>();
            map.put("staff",staff);
            map.put("account", secUserService.getDetail(SecUserObjectTypeEnum.type5, staff.getId()) );
            return ResponseMsgUtil.success(map);

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

    @RequestMapping(value = "/getStaffList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询工作人员列表")
    public ResponseData getStaffList(@RequestParam(name = "agentId", required = false) Long agentId,
                                     @RequestParam(name = "name", required = false) String name,
                                     @RequestParam(name = "telephone", required = false) String telephone,
                                     @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<>();
            if (userSession.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type1.getCode())) {
                param.put("agentId", agentId);
            } else if (userSession.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type4.getCode())) {
                param.put("agentId", userSession.getAccount().getObjectId());
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS, "");
            }
            param.put("name", name);
            param.put("telephone", telephone);

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

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

}
