package com.hzs.activity.declaration.controller.manage;


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.declaration.param.AcDeclarationParam;
import com.hzs.activity.declaration.param.AcDeclarationPkIdParam;
import com.hzs.activity.declaration.service.IAcDeclarationGiftConfigService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author bd
 * @since 2025-10-13
 */
@RestController
@RequestMapping("/manage/acDeclarationConfig")
public class AcDeclarationGiftConfigController extends BaseController {

    @Autowired
    private UserTokenService userTokenService;

    @Autowired
    private IActivityService iActivityService;

    @Autowired
    private IAcDeclarationGiftConfigService iAcDeclarationGiftConfigService;
    /**
     *
     * 报单赠送活动操作
     * @param acDeclarationParam 报单赠送活动
     * @return AjaxResult
     */
    @Log(module = EOperationModule.AC_DECLARATION_GIFT_SAVE, business = EOperationBusiness.AC_DECLARATION_GIFT_SAVE, method = EOperationMethod.INSERT, remark = "报单赠送添加")
    @PostMapping("/save")
    public AjaxResult declarationConfigSave(@RequestBody @Validated AcDeclarationParam acDeclarationParam) {
        acDeclarationParam.setPkCountry(SecurityUtils.getPkCountry());
        acDeclarationParam.setPkMember(SecurityUtils.getUserId());
        acDeclarationParam.setLoginUser(userTokenService.getLoginUser());
        String str = iAcDeclarationGiftConfigService.saveConfig(acDeclarationParam);
        if (str != null) {
            return AjaxResult.error(str);
        }
        return AjaxResult.success();
    }
    /**
     *
     * 报单赠送活动更新
     * @param acDeclarationParam 报单赠送更新
     * @return AjaxResult
     */
    @Log(module = EOperationModule.AC_DECLARATION_GIFT_UPDATE, business = EOperationBusiness.AC_DECLARATION_GIFT_UPDATE, method = EOperationMethod.UPDATE, remark = "报单赠送修改")
    @PostMapping("/update")
    public AjaxResult declarationConfigUpdate(@RequestBody AcDeclarationParam acDeclarationParam) {
        acDeclarationParam.setPkCountry(SecurityUtils.getPkCountry());
        acDeclarationParam.setPkMember(SecurityUtils.getUserId());
        acDeclarationParam.setLoginUser(userTokenService.getLoginUser());
        String str = iAcDeclarationGiftConfigService.updateConfig(acDeclarationParam);
        if (str != null) {
            return AjaxResult.error(str);
        }
        return AjaxResult.success();
    }


    /**
     *
     * 报单赠送活动删除
     * @param  acDeclarationPkIdParam 报单赠送删除
     * @return AjaxResult
     */
    @Log(module = EOperationModule.AC_DECLARATION_GIFT_DELETE, business = EOperationBusiness.AC_DECLARATION_GIFT_DELETE, method = EOperationMethod.DELETE, remark = "报单赠送删除")
    @PostMapping("/delete")
    public AjaxResult declarationConfigDelete(@RequestBody AcDeclarationPkIdParam acDeclarationPkIdParam) {
        if (acDeclarationPkIdParam.getPkId() == null) {
            return AjaxResult.error();
        }
        String str = iAcDeclarationGiftConfigService.deleteConfig(acDeclarationPkIdParam.getPkId());
        if (str != null) {
            return AjaxResult.error(str);
        }
        return AjaxResult.success();
    }

    /**
     * 报单赠送活动详情
     *
     * @param pkId 报单赠送活动详情
     * @return AjaxResult
     */
    @Log(module = EOperationModule.AC_DECLARATION_GIFT_DETAIL, business = EOperationBusiness.AC_DECLARATION_GIFT_DETAIL, method = EOperationMethod.SELECT, remark = "加购升级详情")
    @GetMapping("/get-detail")
    public AjaxResult getDetail(Integer pkId) {
        return iAcDeclarationGiftConfigService.getDetail(pkId);
    }

    /**
     * 报单赠送导出
     */
    @Log(module = EOperationModule.AC_DECLARATION_GIFT_EXPORT, business = EOperationBusiness.AC_DECLARATION_GIFT_DETAIL, method = EOperationMethod.EXPORT, remark = "加购升级导出")
    @PostMapping("/export")
    public void export(QueryActivityParam queryActivityParam, HttpServletResponse response) {
        queryActivityParam.setActType(EActType.DECLARATION_GIFT.getValue());
        queryActivityParam.setPkCountry(SecurityUtils.getPkCountry());
        List<BaseConfigVo> resultList = iActivityService.activityList(queryActivityParam);
        ExcelUtil<BaseConfigVo> util = new ExcelUtil<>(BaseConfigVo.class);
        util.exportExcel(response, resultList, "报单赠送导出");
    }

}
