package com.zbf.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zbf.common.annotation.DataSource;
import com.zbf.common.annotation.Log;
import com.zbf.common.core.controller.BaseController;
import com.zbf.common.core.domain.AjaxResult;
import com.zbf.common.core.page.TableDataInfo;
import com.zbf.common.enums.BusinessType;
import com.zbf.common.enums.DataSourceType;
import com.zbf.common.utils.poi.ExcelUtil;
import com.zbf.system.domain.TCkOrderdetail;
import com.zbf.system.domain.TMiStock;
import com.zbf.system.mapper.TMiStockMapper;
import com.zbf.system.service.ITCkOrderdetailService;
import com.zbf.web.controller.section.EnhanceDataList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.List;

/**
 * 出库物料明细Controller
 *
 * @author sunits
 * @date 2024-06-21
 */
@RestController
@RequestMapping("/system/orderdetail")
public class TCkOrderdetailController extends BaseController {
    @Autowired
    private ITCkOrderdetailService tCkOrderdetailService;
    @Autowired
    private TMiStockMapper tMiStockMapper;

    /**
     * 查询出库物料明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:orderdetail:list')")
    @GetMapping("/list")
    @EnhanceDataList(entityType = TCkOrderdetail.class)
    public TableDataInfo list(TCkOrderdetail tCkOrderdetail) {
        startPage();
        List<TCkOrderdetail> list = tCkOrderdetailService.selectTCkOrderdetailList(tCkOrderdetail);
        return getDataTable(list);
    }

    /**
     * 导出出库物料明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:orderdetail:export')")
    @Log(title = "出库物料明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCkOrderdetail tCkOrderdetail) {
        List<TCkOrderdetail> list = tCkOrderdetailService.selectTCkOrderdetailList(tCkOrderdetail);
        ExcelUtil<TCkOrderdetail> util = new ExcelUtil<TCkOrderdetail>(TCkOrderdetail.class);
        util.exportExcel(response, list, "出库物料明细数据");
    }

    /**
     * 获取出库物料明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:orderdetail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(tCkOrderdetailService.selectTCkOrderdetailById(id));
    }

    /**
     * 新增出库物料明细
     */
    @PreAuthorize("@ss.hasPermi('system:orderdetail:add')")
    @Log(title = "出库物料明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCkOrderdetail tCkOrderdetail) {
        return toAjax(tCkOrderdetailService.insertTCkOrderdetail(tCkOrderdetail));
    }

    /**
     * 修改出库物料明细
     */
    @PreAuthorize("@ss.hasPermi('system:orderdetail:edit')")
    @Log(title = "出库物料明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TCkOrderdetail tCkOrderdetail) {
        return tCkOrderdetailService.updateTCkOrderdetail(tCkOrderdetail);
    }

    /**
     * 删除出库物料明细
     */
    @PreAuthorize("@ss.hasPermi('system:orderdetail:remove')")
    @Log(title = "出库物料明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tCkOrderdetailService.deleteTCkOrderdetailByIds(ids));
    }

    /**
     * 启用
     */
    @PreAuthorize("@ss.hasPermi('system:orderdetail:remove')")
    @Log(title = "出库物料明细", businessType = BusinessType.UPDATE)
    @DeleteMapping("/disable/{ids}")
    public AjaxResult disable(@PathVariable String[] ids) {
        return toAjax(tCkOrderdetailService.disabledTCkOrderdetailByIds(ids));
    }

    /**
     * 禁用
     */
    @PreAuthorize("@ss.hasPermi('system:orderdetail:edit')")
    @Log(title = "出库物料明细", businessType = BusinessType.UPDATE)
    @DeleteMapping("/enable/{ids}")
    public AjaxResult enable(@PathVariable String[] ids) {
        return toAjax(tCkOrderdetailService.enableTCkOrderdetailByIds(ids));
    }

    /**
     * 修改状态
     */
    @PreAuthorize("@ss.hasPermi('system:orderdetail:edit')")
    @Log(title = "出库物料明细", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody TCkOrderdetail tCkOrderdetail) {
        return toAjax(tCkOrderdetailService.updateStatus(tCkOrderdetail));
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<TCkOrderdetail> util = new ExcelUtil<TCkOrderdetail>(TCkOrderdetail.class);
        util.importTemplateExcel(response, "出库单物料明细数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "出库单物料明细", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:orderdetail:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<TCkOrderdetail> util = new ExcelUtil<TCkOrderdetail>(TCkOrderdetail.class);
        InputStream inputStream = file.getInputStream();
        List<TCkOrderdetail> list = util.importExcel(inputStream);
        inputStream.close();
        int count = tCkOrderdetailService.batchInsertTCkOrderdetail(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

}

