package com.bonus.material.ma.controller;

import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.material.ma.domain.StandardConfigBean;
import com.bonus.material.ma.domain.vo.StandardConfigDetailsVo;
import com.bonus.material.ma.service.StandardConfigManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 机械设备标准配置管理Controller
 *
 * @author hay
 */
@Api(tags = "机械设备标准配置管理接口")
@RestController
@RequestMapping("/standardConfig")
public class StandardConfigManageController extends BaseController {

    @Resource
    private StandardConfigManageService service;

    /**
     * 标准配置下拉树
     */
    @ApiOperation(value = "标准配置下拉树")
    @GetMapping("/getConfigTreeSelect")
    public AjaxResult getMaTypeTreeSelect(@RequestParam(required = false, defaultValue = "", value = "configName") String configName,
                                          @RequestParam(required = false, defaultValue = "", value = "parentId") String parentId) {
        return service.getConfigTreeSelect(configName, parentId);
    }

    /**
     * 标准配置下拉选--只有二级数据
     * @param bean
     * @return
     */
    @ApiOperation(value = "标准配置下拉选")
    @PostMapping("getConfigLevelTwoList")
    public AjaxResult getConfigLevelTwoList(@RequestBody StandardConfigBean bean){
        return service.getConfigLevelTwoList(bean);
    }

    /**
     * 根据左配置id查询右表格
     */
    @ApiOperation(value = "根据左配置id查询右表格")
    @GetMapping("/getListByConfigId")
    public AjaxResult getListByConfigId(StandardConfigDetailsVo bean) {
        return service.getListByConfigId(bean);
    }

    /**
     * 导出标准配置管理列表
     * @param response
     * @param bean
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, StandardConfigDetailsVo bean) {
        List<StandardConfigDetailsVo> list = service.selectStandardList(bean);
        ExcelUtil<StandardConfigDetailsVo> util = new ExcelUtil<>(StandardConfigDetailsVo.class);
        util.exportExcel(response, list, "标准配置管理列表数据");
    }
    /**
     * 新增标准配置
     */
    @ApiOperation(value = "新增标准配置")
    @SysLog(title = "机械设备标准配置管理", businessType = OperaType.INSERT, module = "机械设备标准配置管理->新增标准配置")
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody StandardConfigBean bean) {
        return toAjax(service.insertConfig(bean));
    }


    /**
     * 修改标准配置
     */
    @ApiOperation(value = "修改标准配置")
    @SysLog(title = "机械设备标准配置管理", businessType = OperaType.UPDATE, module = "机械设备标准配置管理->修改标准配置")
    @PostMapping(value = "/edit")
    public AjaxResult edit(@RequestBody StandardConfigBean bean) {
        return toAjax(service.updateConfig(bean));
    }

    /**
     * 删除标准配置
     */
    @ApiOperation(value = "删除标准配置")
    @SysLog(title = "机械设备标准配置管理", businessType = OperaType.DELETE, module = "机械设备标准配置管理->删除标准配置")
    @PostMapping("/del")
    public AjaxResult remove(@RequestBody StandardConfigBean bean) {
        return service.deleteConfigByConfigId(bean);
    }

    /**
     * 新增标准配置明细
     */
    @ApiOperation(value = "新增标准配置明细")
    @SysLog(title = "机械设备标准配置管理", businessType = OperaType.INSERT, module = "机械设备标准配置管理->新增标准配置明细")
    @PostMapping(value = "/addConfigDetails")
    public AjaxResult addConfigDetails(@RequestBody StandardConfigDetailsVo bean) {
        try {
            service.addConfigDetails(bean);
            return AjaxResult.success();
        } catch (Exception e) {
            // 这里无需手动回滚，因为@Transactional会自动处理
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改标准配置明细
     */
    @ApiOperation(value = "修改标准配置明细")
    @SysLog(title = "机械设备标准配置管理", businessType = OperaType.UPDATE, module = "机械设备标准配置管理->修改标准配置明细")
    @PostMapping(value = "/editConfigDetails")
    public AjaxResult editConfigDetails(@RequestBody StandardConfigDetailsVo bean) {
        return toAjax(service.updateConfigDetails(bean));
    }

    /**
     * 删除标准配置明细
     */
    @ApiOperation(value = "删除标准配置明细")
    @SysLog(title = "机械设备标准配置管理", businessType = OperaType.DELETE, module = "机械设备标准配置管理->删除标准配置明细")
    @PostMapping("/delConfigDetails")
    public AjaxResult delConfigDetails(@RequestBody StandardConfigDetailsVo bean) {
        return service.delConfigDetails(bean);
    }


    /**
     * 根据配置id查询配置明细
     * 返回参数和规格型号树参数一致
     */
    @ApiOperation(value = "根据配置id查询配置明细")
    @GetMapping("/getListsByConfigId")
    public AjaxResult getListsByConfigId(StandardConfigDetailsVo bean) {
        return service.getListsByConfigId(bean);
    }
}
