package com.hzs.activity.register.controller;/**
 * @Description:
 * @Author: yuhui
 * @Time: 2024/10/9 11:51
 * @Classname: AcRegisterGiftController
 * @PackageName: com.hzs.activity.register.controller
 */

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.activity.base.param.ActivityParam;
import com.hzs.activity.base.service.IAcBaseConfigService;
import com.hzs.activity.register.param.RegisterGiftParam;
import com.hzs.activity.register.service.IAcRegisterGiftService;
import com.hzs.common.core.enums.EActType;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.activity.base.AcBaseConfig;
import com.hzs.common.domain.activity.register.AcRegisterGift;
import com.hzs.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: hzs_java_cloud_africa
 * @BelongsPackage: com.hzs.activity.register.controller
 * @Author: yh
 * @CreateTime: 2024-10-09  11:51
 * @Description: TODO
 * @Version: 1.0
 */
@RequestMapping("/manage/register-gift")
@RestController
public class AcRegisterGiftController extends BaseController {
    @Autowired
    private IAcBaseConfigService baseConfigService;
    @Autowired
    private IAcRegisterGiftService registerGiftService;


    @GetMapping("list")
    public TableDataInfo list() {
        List<AcRegisterGift> list = registerGiftService.list();
        return getDataTable(list);
    }

    /**
     * 新增
     *
     * @param activityParam
     * @return
     */
    @PostMapping("save")
    public AjaxResult save(@RequestBody ActivityParam activityParam) {
        activityParam.setPkCreator(SecurityUtils.getUserId());
        activityParam.setActType(EActType.REGISTER_GIFT.getValue());
        registerGiftService.createRegisterGift(activityParam);
        return AjaxResult.success();
    }


    /**
     * 新增
     *
     * @param activityParam
     * @return
     */
    @PostMapping("update")
    public AjaxResult update(@RequestBody ActivityParam activityParam) {
        activityParam.setActType(EActType.REGISTER_GIFT.getValue());
        activityParam.setPkCreator(SecurityUtils.getUserId());
        registerGiftService.updateRegisterGift(activityParam);
        return AjaxResult.success();
    }

    /**
     * 详情
     *
     * @return
     */
    @GetMapping("detail")
    public AjaxResult detail(Integer pkId) {
        AcBaseConfig baseConfig = baseConfigService.getById(pkId);
        ActivityParam activityParam = BeanUtil.copyProperties(baseConfig, ActivityParam.class);

        QueryWrapper<AcRegisterGift> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_BASE_ID", pkId);
        List<AcRegisterGift> registerGiftParamList = registerGiftService.list(queryWrapper);
        List<RegisterGiftParam> registerGiftParamArray = new ArrayList<>();
        for (AcRegisterGift acRegisterGift : registerGiftParamList) {
            RegisterGiftParam registerGiftParam = BeanUtil.copyProperties(acRegisterGift, RegisterGiftParam.class);
            registerGiftParamArray.add(registerGiftParam);
        }

        activityParam.setRegisterGiftParamList(registerGiftParamArray);
        return AjaxResult.success(registerGiftParamList);

    }

    /**
     * 删除
     */
    @GetMapping("remove")
    public AjaxResult remove(Integer pkId){
        baseConfigService.removeById(pkId);
        registerGiftService.remove(new QueryWrapper<AcRegisterGift>().eq("PK_BASE_ID",pkId));
        return AjaxResult.success();
    }


}
