package com.hzs.activity.recommend.controller;


import com.hzs.activity.base.param.QueryActivityParam;
import com.hzs.activity.base.service.IActivityService;
import com.hzs.activity.base.vo.BaseConfigVo;
import com.hzs.activity.recommend.param.AcRecommendParam;
import com.hzs.activity.recommend.service.IAcRepurCouponsRuleConfigService;
import com.hzs.activity.recommend.vo.AcRecommendVO;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.enums.EActType;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.security.service.UserTokenService;
import com.hzs.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * <p>
 * 直推-复购券规则配置 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2023-04-22
 */
@RestController
@RequestMapping("/manage/recommend")
public class AcRecommendConfigController extends BaseController {
    @Autowired
    private IActivityService iActivityService;

    @Autowired
    IAcRepurCouponsRuleConfigService acRepurCouponsRuleConfigService;

    @Autowired
    private UserTokenService userTokenService;


    /**
     * 在线签呈
     *
     * @param recommendParam 直推参数
     * @return AjaxResult
     */
    @Log(module = EOperationModule.AC_RECOMMEEND_CONFIG, business = EOperationBusiness.AC_RECOMMEEND_CONFIG, method = EOperationMethod.INSERT, remark = "在线签呈")
    @PostMapping("/online-petition")
    public AjaxResult onLinePetition(@RequestBody AcRecommendParam recommendParam) {
        recommendParam.setPkCountry(SecurityUtils.getPkCountry());
        recommendParam.setPkMember(SecurityUtils.getUserId());
        recommendParam.setLoginUser(userTokenService.getLoginUser());
        return toAjax(acRepurCouponsRuleConfigService.onLinePetition(recommendParam));
    }


    /**
     * 直推赠送查看详情
     *
     * @param activityId 活动ID
     * @return AjaxResult
     */
    @Log(module = EOperationModule.AC_RECOMMEEND_CONFIG, business = EOperationBusiness.AC_RECOMMEEND_CONFIG, method = EOperationMethod.SELECT, remark = "直推赠送查看详情")
    @GetMapping("/detail")
    public AjaxResult showDetailInfo(Long activityId) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        return AjaxResult.success(acRepurCouponsRuleConfigService.showDetailInfo(activityId, pkCountry));
    }


    /**
     * 直推赠送删除
     *
     * @param activityId 活动ID
     * @return AjaxResult
     */
    @Log(module = EOperationModule.AC_RECOMMEEND_CONFIG, business = EOperationBusiness.AC_RECOMMEEND_CONFIG, method = EOperationMethod.DELETE, remark = "直推赠送删除")
    @GetMapping("/recommend-delete")
    public AjaxResult deleteRecommendConfig(Long activityId) {
        return AjaxResult.success(acRepurCouponsRuleConfigService.deleteRecommendConfig(activityId));
    }


    /**
     * 保存复购券规则配置
     *
     * @param recommendVO 直推参数
     * @return AjaxResult
     */
    @Log(module = EOperationModule.AC_RECOMMEEND_CONFIG, business = EOperationBusiness.AC_RECOMMEEND_CONFIG, method = EOperationMethod.UPDATE)
    @PostMapping("/recommend-update")
    public AjaxResult updateRecommendConfig(@RequestBody AcRecommendVO recommendVO) {
        recommendVO.setPkCountry(SecurityUtils.getPkCountry());
        recommendVO.setPkMember(SecurityUtils.getUserId());
        return toAjax(acRepurCouponsRuleConfigService.updateRecommendConfig(recommendVO));
    }

    /**
     * 直推赠送导出
     */
    @PostMapping("export")
    public void export (QueryActivityParam queryActivityParam, HttpServletResponse response){
        queryActivityParam.setActType(EActType.DIRECT_PUSH_GIFT.getValue());
        queryActivityParam.setPkCountry(SecurityUtils.getPkCountry());
        List<BaseConfigVo> resultList = iActivityService.activityList(queryActivityParam);
        ExcelUtil<BaseConfigVo> util = new ExcelUtil<>(BaseConfigVo.class);
        util.exportExcel(response, resultList, "直推赠送导出");
    }

}
