package com.cpop.jambox.business.controller.mini;

import com.cpop.core.base.R;
import com.cpop.core.utils.SpringUtils;
import com.cpop.jambox.business.bo.CardTemplateUnionBo;
import com.cpop.jambox.business.entity.CardTemplate;
import com.cpop.jambox.business.service.CardTemplateExtendService;
import com.cpop.jambox.business.service.CardTemplateService;
import com.cpop.jambox.business.vo.CardTemplateInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.cpop.jambox.business.entity.table.CardTemplateExtendTableDef.CARD_TEMPLATE_EXTEND;
import static com.cpop.jambox.business.entity.table.CardTemplateTableDef.CARD_TEMPLATE;

/**
 * 果酱-课卡模板 控制层。
 *
 * @author DB
 * @since 2023-09-27
 */
@RestController
@Api(tags = "果酱-课卡模板接口")
@RequestMapping("/mini/cardTemplate")
public class MiniCardTemplateController {

    @Autowired
    private CardTemplateService cardTemplateService;

    /**
     * 添加课卡模板(整合)
     * @author DB
     * @since 2023/10/07 9:59
     * @param bo 请求参数
     */
    //@SimpleSignatureCheck
    @ApiOperation(value = "添加课卡模板(整合)")
    @PostMapping("/insertCardTemplateUnion")
    public R<Void> insertCardTemplateUnion(@RequestBody @Validated CardTemplateUnionBo bo) {
        cardTemplateService.insertCardTemplateUnion(bo);
        return R.ok();
    }

    /**
     * 修改课卡模板(整合)
     * @author DB
     * @since 2023/10/07 9:59
     * @param bo 请求参数
     */
    //@SimpleSignatureCheck
    @ApiOperation(value = "修改课卡模板(整合)")
    @PutMapping("/updateCardTemplateUnion")
    public R<Void> updateCardTemplateUnion(@RequestBody @Validated CardTemplateUnionBo bo) {
        cardTemplateService.updateCardTemplateUnion(bo);
        return R.ok();
    }

    /**
     * 删除课卡模板
     * @author DB
     * @since 2024/1/19
     * @param oldId 旧主键id
     * @return R<Void>
     */
    @Deprecated
    @ApiOperation(value = "删除课卡模板")
    @DeleteMapping("/removeCardTemplateById/{oldId}")
    public R<Void> removeCardTemplateById(@PathVariable String oldId) {
        CardTemplate cardTemplate = cardTemplateService.queryChain().where(CARD_TEMPLATE.OLD_TEMPLATE_ID.eq(oldId)).one();
        cardTemplateService.updateChain()
                .where(CARD_TEMPLATE.OLD_TEMPLATE_ID.eq(oldId))
                .remove();
        SpringUtils.getBean(CardTemplateExtendService.class).updateChain()
                .where(CARD_TEMPLATE_EXTEND.TEMPLATE_ID.eq(cardTemplate.getId()))
                .remove();
        return R.ok();
    }

    /**
     * 获取模板信息
     * @author DB
     * @since 2024/1/22
     * @param id 主键
     * @return R<Void>
     */
    @ApiOperation(value = "获取模板信息")
    @GetMapping("/getCardTemplateInfo")
    public R<CardTemplateInfoVo> getCardTemplateInfo(@ApiParam("id") @RequestParam(value = "id", required = false) String id,
                                                     @ApiParam("旧模板id") @RequestParam(value = "oldTemplateId", required = false) String oldTemplateId) {
        CardTemplateInfoVo vo = cardTemplateService.getCardTemplateInfo(id, oldTemplateId);
        return R.ok(vo);
    }
}
