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.config.CommonSysConst;
import com.hfkj.entity.BsMer;
import com.hfkj.entity.BsMerPlatformNo;
import com.hfkj.entity.BsSalesman;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserInfoModel;
import com.hfkj.obs.HuaWeiYunObs;
import com.hfkj.service.BsMerPlatformNoService;
import com.hfkj.service.BsMerService;
import com.hfkj.service.BsSalesmanService;
import com.hfkj.service.SecUserService;
import com.hfkj.sysenum.PlatformTypeEnum;
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 org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.util.*;

@Controller
@RequestMapping(value = "/mer")
@Api(value = "商户")
public class BsMerController {

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

    @Resource
    private BsMerService merService;

    @Resource
    private BsSalesmanService salesmanService;

    @Resource
    private UserCenter userCenter;

    @RequestMapping(value="/getMerList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商户列表")
    public ResponseData getMerList(@RequestParam(value = "agentId" , required = false) Long agentId,
                                   @RequestParam(value = "salesmanId" , required = false) Long salesmanId,
                                   @RequestParam(value = "merType" , required = false) Integer merType,
                                   @RequestParam(value = "merNo" , required = false) String merNo,
                                   @RequestParam(value = "merName" , required = false) String merName,
                                   @RequestParam(value = "regName" , required = false) String regName,
                                   @RequestParam(value = "regPhone" , required = false) String regPhone,
                                   @RequestParam(value = "merStatus" , required = false) Integer merStatus,
                                   @RequestParam(value = "pageNum" , required = true) Integer pageNum,
                                   @RequestParam(value = "pageSize" , required = true) Integer pageSize
    ) {
        try {
            UserInfoModel userModel = userCenter.getSessionModel(UserInfoModel.class);

            Map<String,Object> map = new HashMap<>();
            map.put("merType", merType);
            map.put("merNo", merNo);
            map.put("merName", merName);
            map.put("merStatus", merStatus);
            map.put("regName", regName);
            map.put("regPhone", regPhone);

            if (userModel.getSecUser().getObjectType().equals(SecUserTypeEnum.type1.getNumber())) {
                map.put("agentId", agentId);
                map.put("salesmanId", salesmanId);
            }

            if (userModel.getSecUser().getObjectType().equals(SecUserTypeEnum.type2.getNumber())) {
                map.put("agentId", userModel.getAgent().getId());
                map.put("salesmanId", salesmanId);
            }

            if (userModel.getSecUser().getObjectType().equals(SecUserTypeEnum.type3.getNumber())) {
                map.put("salesmanId", userModel.getSalesman().getId());
            }

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(merService.getMerList(map)));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getMerDetail",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商户详情")
    public ResponseData getMerDetail(@RequestParam(value = "merId" , required = true) Long merId) {
        try {

            return ResponseMsgUtil.success(merService.getMerDetail(merId));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/merMigrateSalesman",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "商户迁移业务员")
    public ResponseData merMigrateSalesman(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || body.getLong("salesmanId") == null
                    || body.getJSONArray("merList") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 业务员
            BsSalesman salesman = salesmanService.getSalesman(body.getLong("salesmanId"));
            if (salesman == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "业务员不存在");
            }

            for (Object merId : body.getJSONArray("merList")) {
                // 商户
                BsMer mer = merService.getMer(Long.parseLong(merId.toString()));
                if (mer == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "商户不存在");
                }
                mer.setSalesmanId(salesman.getId());
                mer.setSalesmanName(salesman.getName());
                merService.editMer(mer);
            }

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

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }
}
