package com.cpop.oam.business.controller.backstage;

import com.cpop.common.utils.bean.BeanUtils;
import com.cpop.core.base.R;
import com.cpop.jambox.business.bo.PlugBo;
import com.cpop.jambox.business.entity.StorePlug;
import com.cpop.jambox.business.service.PlugService;
import com.cpop.jambox.business.service.StorePlugService;
import com.cpop.jambox.business.vo.PlugPageVo;
import com.cpop.jambox.business.vo.PlugVo;
import com.cpop.jambox.business.vo.StorePlugListVo;
import com.cpop.oam.business.bo.ChangePlugOpenBo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
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 java.util.List;

import static com.cpop.jambox.business.entity.table.PlugTableDef.PLUG;
import static com.cpop.jambox.business.entity.table.StorePlugTableDef.STORE_PLUG;

/**
 * 校区-插件记录表 控制层。
 *
 * @author DB
 * @since 2023-12-29
 */
@RestController
@Api(tags = "校区-插件记录表接口")
@RequestMapping("/backstage/storePlug")
public class BackstageStorePlugController {

    @Autowired
    private StorePlugService storePlugService;

    @Autowired
    private PlugService plugService;

    /**
     * 查询插件分页
     * @author DB
     * @since 2024/1/5
     * @return R<Page<PlugPageVo>>
     */
    @GetMapping("/getPlugPage")
    @ApiOperation("查询插件分页")
    public R<Page<PlugPageVo>> getPlugPage() {
        Page<PlugPageVo> page = plugService.getPlugPage();
        return R.ok(page);
    }

    /**
     * 查询插件详情
     * @author DB
     * @since 2024/1/5
     * @param id 主键
     * @return R<PlugVo>
     */
    @GetMapping("/getPlugById/{id}")
    @ApiOperation("查询插件详情")
    public R<PlugVo> getPlugById(@PathVariable String id) {
        PlugVo vo = plugService.getOneAs(QueryWrapper.create().where(PLUG.ID.eq(id)), PlugVo.class);
        return R.ok(vo);
    }

    /**
     * 新增插件
     * @author DB
     * @since 2024/1/5
     * @param bo 请求参数
     * @return R<Void>
     */
    @ApiOperation("新增插件")
    @PostMapping("/insertPlug")
    public R<Void> insertPlug(@RequestBody @Validated PlugBo bo) {
        plugService.insertPlug(bo);
        return R.ok();
    }

    /**
     * 修改插件
     * @author DB
     * @since 2024/1/5
     * @param bo 请求参数
     * @return R<Void>
     */
    @ApiOperation("修改插件")
    @PutMapping("/updatePlug")
    public R<Void> updateStaff(@RequestBody @Validated PlugBo bo) {
        plugService.updatePlug(bo);
        return R.ok();
    }

    /**
     * 删除插件
     * @author DB
     * @since 2024/1/5
     * @param id 主键
     * @return R<Void>
     */
    @ApiOperation("删除插件")
    @DeleteMapping("/removePlugById/{id}")
    public R<Void> removeStaffById(@PathVariable String id) {
        plugService.removeById(id);
        return R.ok();
    }

    /**
     * 查询校区插件列表
     *
     * @param brandId brandId
     * @param storeId storeId
     */
    @GetMapping("/getStorePlugList")
    @ApiOperation("查询校区插件列表")
    public R<List<StorePlugListVo>> getStorePlugList(@ApiParam("品牌id") String brandId, @ApiParam("校区id") String storeId) {
        List<StorePlugListVo> list = storePlugService.getStorePlugList(brandId, storeId);
        return R.ok(list);
    }

    /**
     * 插件开启与关闭
     */
    @PutMapping("/changePlugOpen")
    @ApiOperation("插件开启与关闭")
    public R<Void> changePlugOpen(@RequestBody @Validated ChangePlugOpenBo bo) {
        StorePlug storePlug = BeanUtils.mapToClass(bo, StorePlug.class);
        if (bo.getIsOpen()){
            storePlugService.save(storePlug);
        } else {
            storePlugService.updateChain().where(STORE_PLUG.STORE_ID.eq(bo.getStoreId())).and(STORE_PLUG.PLUG_TAG.eq(bo.getPlugTag())).remove();
        }
        return R.ok();
    }

}
