package com.taais.mf.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.taais.common.core.core.domain.CommonResult;
import com.taais.common.excel.utils.ExcelUtil;
import com.taais.common.log.annotation.Log;
import com.taais.common.log.enums.BusinessType;
import com.taais.common.web.annotation.RepeatSubmit;
import com.taais.common.web.core.BaseController;
import com.taais.mf.domain.bo.MfProductBo;
import com.taais.mf.domain.vo.MfProductVo;
import com.taais.mf.service.IMfProductService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 产品树Controller
 *
 * @author wgk
 * 2024-01-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mf/product")
public class MfProductController extends BaseController {
    @Resource
    private IMfProductService mfProductService;

    /**
     * 查询产品树列表
     */
    @SaCheckPermission("mf:product:list")
    @GetMapping("/list")
    public CommonResult<List<MfProductVo>> list(MfProductBo mfProductBo) {
        List<MfProductVo> list = mfProductService.selectList(mfProductBo);
        return CommonResult.success(list);
    }

    /**
     * 导出产品树列表
     */
    @SaCheckPermission("mf:product:export")
    @Log(title = "产品树", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MfProductBo mfProductBo) {
        List<MfProductVo> list = mfProductService.selectList(mfProductBo);
        ExcelUtil.exportExcel(list, "产品树", MfProductVo.class, response);
    }

    /**
     * 获取产品树详细信息
     */
    @SaCheckPermission("mf:product:query")
    @GetMapping(value = "/{productId}")
    public CommonResult<MfProductVo> getInfo(@PathVariable Long productId) {
        return CommonResult.success(mfProductService.selectById(productId));
    }

    /**
     * 新增产品树
     */
    @SaCheckPermission("mf:product:add")
    @Log(title = "产品树", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody MfProductBo mfProductBo) {
        boolean inserted = mfProductService.insert(mfProductBo);
        if (!inserted) {
            return CommonResult.fail("新增产品树记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改产品树
     */
    @SaCheckPermission("mf:product:edit")
    @Log(title = "产品树", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody MfProductBo mfProductBo) {
        Boolean updated = mfProductService.update(mfProductBo);
        if (!updated) {
            return CommonResult.fail("修改产品树记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除产品树
     */
    @SaCheckPermission("mf:product:remove")
    @Log(title = "产品树", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productIds}")
    public CommonResult<Void> remove(@PathVariable Long[] productIds) {
        boolean deleted = mfProductService.deleteByIds(productIds);
        if (!deleted) {
            return CommonResult.fail("删除产品树记录失败!");
        }
        return CommonResult.success();
    }
}
