package com.cweb.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.MD5Util;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsSalesman;
import com.hfkj.entity.SecUser;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserInfoModel;
import com.hfkj.service.BsMerService;
import com.hfkj.service.BsSalesmanService;
import com.hfkj.service.SecUserService;
import com.hfkj.sysenum.SalesmanStatusEnum;
import com.hfkj.sysenum.SecUserTypeEnum;
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 = "/salesman")
@Api(value = "代理商")
public class BsSalesmanController {

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

    @Resource
    private UserCenter userCenter;

    @Resource
    private BsSalesmanService salesmanService;

    @Resource
    private BsMerService merService;

    @RequestMapping(value = "/moveHomeCount", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "移动端首页统计")
    public ResponseData moveHomeCount() {
        try {
            UserInfoModel userModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userModel.getSalesman() == null) {
                log.error("BsSalesmanController --> insertSalesman() error!", "权限不足");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMPETENCE_INSUFFICIENT, "");
            }
            Map<String, Object> param = new HashMap<>();

            Map<String, Object> map = new HashMap<>();
            map.put("salesmanId", userModel.getSalesman().getId());
            map.put("completeStatus", 1);
            map.put("merStatus", 1);
            // 进件成功数量
            param.put("contractSuccess", merService.countMerSum(map));

            map.clear();
            map.put("salesmanId", userModel.getSalesman().getId());
            map.put("completeStatus", 1);
            map.put("merStatus", 3);
            // 进件审核数量
            param.put("contractAudit", merService.countMerSum(map));

            return ResponseMsgUtil.success(param);

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

    @RequestMapping(value = "/querySalesmanList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询业务员列表")
    public ResponseData querySalesmanList(@RequestParam(value = "agentId", required = false) String agentId,
                                         @RequestParam(value = "name", required = false) String name,
                                         @RequestParam(value = "phone", required = false) String phone,
                                         @RequestParam(value = "status", required = false) Integer status,
                                         @RequestParam(value = "pageNum", required = true) Integer pageNum,
                                         @RequestParam(value = "pageSize", required = true) Integer pageSize) {
        try {

            Map<String, Object> param = new HashMap<>();
            param.put("agentId", agentId);
            param.put("name", name);
            param.put("phone", phone);
            param.put("status", status);

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(salesmanService.getSalesmanList(param)));
        } catch (Exception e) {
            log.error("BsSalesmanController --> querySalesmanList() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }
}
