package com.cweb.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.channel.saas.SaasActivityService;
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.BsAudit;
import com.hfkj.entity.BsBankActivity;
import com.hfkj.entity.BsBankActivityRoster;
import com.hfkj.entity.BsBankActivityRosterRate;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserInfoModel;
import com.hfkj.service.BsAuditService;
import com.hfkj.service.BsBankActivityRosterRateService;
import com.hfkj.service.BsBankActivityRosterService;
import com.hfkj.service.BsBankActivityService;
import com.hfkj.sysenum.AuditStatusEnum;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Api(value = "银行权益活动")
@RequestMapping(value = "/bankActivity")
public class BsBankActivityController {

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

    @Resource
    private UserCenter userCenter;
    @Resource
    private BsAuditService auditService;
    @Resource
    private SaasActivityService saasActivityService;
    @Resource
    private BsBankActivityService bankActivityService;
    @Resource
    private BsBankActivityRosterService bsBankActivityRosterService;
    @Resource
    private BsBankActivityRosterRateService bankActivityRosterRateService;

    @RequestMapping(value="/refreshActivityList",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取活动列表")
    public ResponseData refreshActivityList() {
        try {

            JSONArray jsonArray = saasActivityService.queryAgentActivityList();
            for (Object obj : jsonArray) {
               JSONObject activity = (JSONObject) obj;

                BsBankActivity bankActivity = bankActivityService.getBankActivityByNo(activity.getLong("activityId"));
                if (bankActivity == null) {
                    bankActivity = new BsBankActivity();
                }
                bankActivity.setRegions(activity.getString("regions"));
                bankActivity.setActivityNo(activity.getLong("activityId"));
                bankActivity.setActivityName(activity.getString("activityName"));
                bankActivity.setEffectiveStart(activity.getDate("effectiveStart"));
                bankActivity.setEffectiveEnd(activity.getDate("effectiveEnd"));
                bankActivity.setRegistrationStart(activity.getDate("registrationStart"));
                bankActivity.setRegistrationEnd(activity.getDate("registrationEnd"));
                bankActivity.setDetails(activity.getString("details"));
                bankActivity.setComments(activity.getString("comments"));
                bankActivity.setStatus(1);
                bankActivityService.editData(bankActivity);
            }
            return ResponseMsgUtil.success("操作成功");

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


    @RequestMapping(value="/queryActivityList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取活动列表")
    public ResponseData queryActivityList(@RequestParam(value = "merId" , required = true) Long merId) {
        try {

            List<BsBankActivity> activityList = bankActivityService.getBankActivityList(new HashMap<>());
            for (BsBankActivity bankActivity : activityList) {
                bankActivity.setBankActivityRoster(bsBankActivityRosterService.getActivityRoster(bankActivity.getId(), merId));
            }
            return ResponseMsgUtil.success(activityList);

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

    @RequestMapping(value="/queryActivityDetail",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取活动详情")
    public ResponseData queryActivityDetail(@RequestParam(value = "bankActivityId" , required = true) Long bankActivityId,
                                            @RequestParam(value = "merId" , required = true) Long merId) {
        try {

            BsBankActivity bankActivity = bankActivityService.getBankActivityById(bankActivityId);
            if (bankActivity != null) {
                bankActivity.setBankActivityRoster(bsBankActivityRosterService.getActivityRoster(bankActivityId, merId));
            }
            return ResponseMsgUtil.success(bankActivity);

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


    @RequestMapping(value="/enrollActivity",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "报名活动")
    public ResponseData enrollActivity(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || body.getLong("merId") == null
                    || body.getLong("bankActivityId") == null
                    || StringUtils.isBlank(body.getString("branchBankNo"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            bsBankActivityRosterService.enrollActivity(body.getLong("bankActivityId"), body.getLong("merId"), body.getString("branchBankNo"));

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

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


    @RequestMapping(value="/configRosterRate",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "配置费率")
    public ResponseData configRosterRate(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || body.getLong("bankActivityId") == null
                    || body.getLong("merId") == null
                    || body.getJSONArray("rateList") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            List<BsBankActivityRosterRate> bankActivityRosterRateList = new ArrayList<>();

            for (Object obj : body.getJSONArray("rateList")) {
                JSONObject rate = (JSONObject) obj;
                if (rate.getLong("rateId") == null || rate.getBigDecimal("preferentialRates") == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
                BsBankActivityRosterRate activityRosterRate = bankActivityRosterRateService.getRosterRateById(rate.getLong("rateId"));
                if (activityRosterRate == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
                activityRosterRate.setPreferentialRates(rate.getBigDecimal("preferentialRates").toString());
                bankActivityRosterRateList.add(activityRosterRate);
            }

            bsBankActivityRosterService.configRosterRate(body.getLong("bankActivityId"), body.getLong("merId"), bankActivityRosterRateList);

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

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


    @RequestMapping(value="/getRosterRateList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询费率")
    public ResponseData getRosterRateList(@RequestParam(value = "bankActivityId" , required = true) Long bankActivityId,
                                          @RequestParam(value = "merId" , required = true) Long merId) {
        try {

            return ResponseMsgUtil.success(bankActivityRosterRateService.getListByMerId(bankActivityId, merId));

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


}
