package com.zbf.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.utils.poi.ExcelUtil;
import com.zbf.system.domain.TMiStock;
import com.zbf.system.domain.TPanOrder;
import com.zbf.system.service.ITMiStockService;
import com.zbf.system.service.ITPanDetailService;
import com.zbf.system.service.ITPanOrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 盘点单Controller
 *
 * @author wgs
 * @date 2024-06-20
 */
@RestController
@RequestMapping("/system/pan/order")
public class TPanOrderController extends BaseController {
    private final ITPanOrderService tPanOrderService;
    private final ITPanDetailService detailService;
    private ITMiStockService stockService;

    public TPanOrderController(ITPanOrderService tPanOrderService, ITPanDetailService detailService, ITMiStockService stockService) {
        this.tPanOrderService = tPanOrderService;
        this.detailService = detailService;
        this.stockService = stockService;
    }


    /**
     * 保存单据主表跟单据从表的数据
     */
    /*@PreAuthorize("@ss.hasPermi('system:pan:list')")
    @Log(title = "保存盘点单主从表", businessType = BusinessType.EXPORT)
    @PostMapping("/saveAll")
    public AjaxResult saveAll(@RequestBody TPanVo panVo) {

        TPanOrder panOrder = new TPanOrder();
        BeanUtils.copyProperties(panVo, panOrder);
        tPanOrderService.saveOrUpdate(panOrder);
        List<TPanDetail> detailList = panVo.getDetailList();
        detailList = detailList.stream().map(obj -> {
            obj.setMainId(panOrder.getId());
            return obj;
        }).collect(Collectors.toList());
        detailService.saveOrUpdateBatch(detailList);
        return AjaxResult.success();
    }*/


    /**
     * 根据当前库存，生成盘点明细
     */
    @PreAuthorize("@ss.hasPermi('system:pan:list')")
    @GetMapping("/importCurrStock")
    public TableDataInfo importCurrStock(@RequestParam Map<String, Object> params) {
        startPage();
        LambdaQueryWrapper<TMiStock> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(TMiStock::getStorageId, params.getOrDefault("storageId", ""));
        queryWrapper.eq(TMiStock::getStorageAreaId, params.getOrDefault("storageAreaId", ""));
        List<TMiStock> list = stockService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询盘点单列表 importCurrStock
     */
    @PreAuthorize("@ss.hasPermi('system:pan:list')")
    @GetMapping("/list")
    public TableDataInfo list(TPanOrder tPanOrder) {
        startPage();
        tPanOrder.setDeptId(getDeptId());
        List<TPanOrder> list = tPanOrderService.selectTPanOrderList(tPanOrder);
        return getDataTable(list);
    }

    /**
     * 导出盘点单列表
     */
    @PreAuthorize("@ss.hasPermi('system:pan:list')")
    @Log(title = "盘点单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TPanOrder tPanOrder) {
        tPanOrder.setDeptId(getDeptId());
        List<TPanOrder> list = tPanOrderService.selectTPanOrderList(tPanOrder);
        ExcelUtil<TPanOrder> util = new ExcelUtil<TPanOrder>(TPanOrder.class);
        util.exportExcel(response, list, "盘点单数据");
    }

    /**
     * 获取盘点单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:pan:list')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(tPanOrderService.getById(id));
    }

    /**
     * 新增盘点单
     */
    @PreAuthorize("@ss.hasPermi('system:pan:list')")
    @Log(title = "盘点单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TPanOrder tPanOrder) {
        tPanOrder.setDeptId(getDeptId());
        return toAjax(tPanOrderService.save(tPanOrder));
    }

    /**
     * 修改盘点单
     */
    @PreAuthorize("@ss.hasPermi('system:pan:list')")
    @Log(title = "盘点单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TPanOrder tPanOrder) {
        return toAjax(tPanOrderService.updateById(tPanOrder));
    }

    /**
     * 删除盘点单
     */
    @PreAuthorize("@ss.hasPermi('system:pan:list')")
    @Log(title = "盘点单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tPanOrderService.removeById(ids));
    }

    /**
     * 启用
     */
    @PreAuthorize("@ss.hasPermi('system:pan:list')")
    @Log(title = "盘点单", businessType = BusinessType.UPDATE)
    @DeleteMapping("/disable/{ids}")
    public AjaxResult disable(@PathVariable String[] ids) {
        List<TPanOrder> list = new ArrayList<>();
        for (String id : ids) {
            TPanOrder tPanOrder = new TPanOrder();
            tPanOrder.setId(id);
            tPanOrder.setDelFlag(1);
        }
        return toAjax(tPanOrderService.updateBatchById(list));
    }

    /**
     * 修改状态
     */
    @PreAuthorize("@ss.hasPermi('system:pan:list')")
    @Log(title = "盘点单", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody TPanOrder tPanOrder) {
        return toAjax(tPanOrderService.updateById(tPanOrder));
    }

    /**
     * 手持盘点提交
     */
    @Log(title = "手持盘点", businessType = BusinessType.UPDATE)
    @PutMapping("/pdaPanConfirm")
    public AjaxResult pdaPanConfirm(@RequestBody List<TMiStock> tMiStockList) {
        return tPanOrderService.pdaPanConfirm(tMiStockList);
    }
}

