package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
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.entity.HighTySalesman;
import com.hai.enum_type.UserObjectTypeEnum;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.HighTyAgentService;
import com.hai.service.HighTySalesmanService;
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 = "/tySalesman")
@Api(value = "订单接口")
public class HighTySalesmanController {

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

    @Resource
    private HighTySalesmanService tySalesmanService;

    @Resource
    private HighTyAgentService tyAgentService;

    @Resource
    private UserCenter userCenter;

    @RequestMapping(value = "/addSalesman", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "增加业务员")
    public ResponseData addSalesman(@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.getHighTyAgent() == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法创建，权限不足");
            }
            if (StringUtils.isBlank(body.getString("salesmanName"))
                    || StringUtils.isBlank(body.getString("salesmanPhone"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQUEST_ERROR, "");
            }

            HighTySalesman tySalesman = new HighTySalesman();
            tySalesman.setTyAgentId(userModel.getHighTyAgent().getId());
            tySalesman.setSalesmanName(body.getString("salesmanName"));
            tySalesman.setSalesmanPhone(body.getString("salesmanPhone"));
            tySalesmanService.addSalesman(tySalesman);

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

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

    @RequestMapping(value = "/updateSalesman", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改业务员")
    public ResponseData updateSalesman(@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.getSecUser().getObjectType().equals(UserObjectTypeEnum.type6.getType())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法修改，权限不足");
            }
            if (body.getLong("id") == null
                    || StringUtils.isBlank(body.getString("salesmanName"))
                    || StringUtils.isBlank(body.getString("salesmanPhone"))
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQUEST_ERROR, "");
            }

            // 查询代理商
            HighTySalesman salesman = tySalesmanService.getDetailById(body.getLong("id"));
            if (salesman == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到代理商信息");
            }
            salesman.setSalesmanName(body.getString("salesmanName"));
            salesman.setSalesmanPhone(body.getString("salesmanPhone"));
            tySalesmanService.updateSalesman(salesman);

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

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

    @RequestMapping(value = "/delSalesman", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除业务员")
    public ResponseData delSalesman(@RequestBody JSONObject body, HttpServletRequest request) {
        try {
            if (StringUtils.isBlank(body.getString("salesmanKey"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQUEST_ERROR, "");
            }
            tySalesmanService.delSalesman(body.getString("salesmanKey"));

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

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

    @RequestMapping(value = "/salesmanPwdReset", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "业务员密码重置")
    public ResponseData salesmanPwdReset(@RequestBody JSONObject body, HttpServletRequest request) {
        try {
            if (StringUtils.isBlank(body.getString("salesmanKey"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQUEST_ERROR, "");
            }
            tySalesmanService.salesmanPwdReset(body.getString("salesmanKey"));

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

        } catch (Exception e) {
            log.error("HighTyAgentController --> salesmanPwdReset() 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(tySalesmanService.getDetailByKey(key));

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

    @RequestMapping(value = "/getSalesmanList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询业务员列表")
    public ResponseData getSalesmanList(@RequestParam(name = "tyAgentId", required = true) Long tyAgentId,
                                        @RequestParam(name = "salesmanKey", required = false) String salesmanKey,
                                        @RequestParam(name = "salesmanName", required = false) String salesmanName,
                                        @RequestParam(name = "salesmanPhone", required = false) String salesmanPhone,
                                        @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                        @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {

            Map<String, Object> param = new HashMap<>();
            param.put("tyAgentId", tyAgentId);
            param.put("salesmanKey", salesmanKey);
            param.put("salesmanName", salesmanName);
            param.put("salesmanPhone", salesmanPhone);

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

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

}
