package com.ruoyi.inventory.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.code.QRCodeUtils;
import com.ruoyi.common.code.QrDTO;
import com.ruoyi.common.utils.file.Folder2ZipUtils;
import com.ruoyi.inventory.domain.dto.DepAssetLossResultDto;
import com.ruoyi.inventory.domain.dto.TakeStockDTO;
import com.ruoyi.inventory.domain.dto.TbAssetInventoryDTO;
import com.ruoyi.inventory.domain.enums.InventoryPlanResult;
import com.ruoyi.inventory.domain.enums.InventoryPlanStatus;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.inventory.domain.TbAssetInventory;
import com.ruoyi.inventory.service.ITbAssetInventoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 资产盘点记录Controller
 *
 * @author ydl
 * @date 2023-06-01
 */
@RestController
@RequestMapping("/inventory/inventory")
public class TbAssetInventoryController extends BaseController
{
    @Autowired
    private ITbAssetInventoryService tbAssetInventoryService;


    /**
     * 查询资产盘点记录列表
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbAssetInventory tbAssetInventory)
    {
        startPage();
        List<TbAssetInventory> list = tbAssetInventoryService.selectTbAssetInventoryList(tbAssetInventory);
        return getDataTable(list);
    }

    /**
     * 查询资产盘点记录列表
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:list')")
    @GetMapping("/listDTO")
    public TableDataInfo listDTO(TbAssetInventoryDTO dto)
    {
        startPage();
        List<TbAssetInventoryDTO> list = tbAssetInventoryService.selectTbAssetInventoryDTOList(dto);
        return getDataTable(list);
    }

    /**
     * 导出资产盘点记录列表
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:export')")
    @Log(title = "资产盘点记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbAssetInventory tbAssetInventory)
    {
        List<TbAssetInventory> list = tbAssetInventoryService.selectTbAssetInventoryList(tbAssetInventory);
        for (TbAssetInventory assetInventory : list) {
            Integer inventoryStatus = assetInventory.getInventoryStatus();
            assetInventory.setInventoryStatusName(InventoryPlanStatus.getName(inventoryStatus));

            Integer inventoryResult = assetInventory.getInventoryResult();
            assetInventory.setInventoryResultName(InventoryPlanResult.getName(inventoryResult));

            Integer recordStatus = assetInventory.getRecordStatus();
            String recordStatusName = recordStatus > 0 ? "已提交" : "未提交";
            assetInventory.setRecordStatusName(recordStatusName);
        }
        ExcelUtil<TbAssetInventory> util = new ExcelUtil<TbAssetInventory>(TbAssetInventory.class);
        util.exportExcel(response, list, "资产盘点记录数据");
    }

    /**
     * 获取资产盘点记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbAssetInventoryService.selectTbAssetInventoryById(id));
    }

    /**
     * 获取资产盘点记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:query')")
    @GetMapping(value = "/info/{orderNumber}")
    public AjaxResult getInfo2(@PathVariable("orderNumber") String orderNumber)
    {
        return success(tbAssetInventoryService.selectTbAssetInventoryByNumber(orderNumber));
    }

    /**
     * 新增资产盘点记录
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:add')")
    @Log(title = "资产盘点记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbAssetInventory tbAssetInventory)
    {
        return toAjax(tbAssetInventoryService.insertTbAssetInventory(tbAssetInventory));
    }

    /**
     * 手持机盘点
     * @param dto
     * @return
     */
    @PostMapping("/takeStock")
    public AjaxResult addByTakeStock(@RequestBody List<TakeStockDTO> dto){
        // int result = tbAssetInventoryService.addByTakeStock(dto);
        int result = tbAssetInventoryService.handheldInventory(dto);
        return toAjax(result);
    }

    /**
     * 修改资产盘点记录
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:edit')")
    @Log(title = "资产盘点记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbAssetInventory tbAssetInventory)
    {
        return toAjax(tbAssetInventoryService.updateTbAssetInventory(tbAssetInventory));
    }

    /**
     * 删除资产盘点记录
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:remove')")
    @Log(title = "资产盘点记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbAssetInventoryService.deleteTbAssetInventoryByIds(ids));
    }

    /**
     * 查看计划二维码
     */
    @GetMapping(value = "/code/{number}", produces = MediaType.IMAGE_PNG_VALUE)
    public void selectQrCode(HttpServletResponse response, @PathVariable("number") String orderNumber, @RequestParam("v") String v) throws IOException {
        TbAssetInventory tbAssetInventory = tbAssetInventoryService.selectTbAssetInventoryByNumber(orderNumber);
        if (tbAssetInventory == null) {
            return;
        }
        QrDTO qrDTO = new QrDTO();
        qrDTO.setName(tbAssetInventory.getName());
        qrDTO.setValue(tbAssetInventory.getOrderNumber());
        QRCodeUtils.createCodeToOutputStream(qrDTO, response.getOutputStream());
    }

//    /**
//     * 查询部门资产报表（两次盘点间的资产损耗）
//     * @param depId 部门ID
//     * @return
//     */
////    我粘贴过来的 @PreAuthorize("@ss.hasPermi('inventory:inventory:query')")
//    @GetMapping("/getDepAssetLossTb")
//    public AjaxResult getDepAssetLossTb(String depId){
//        List<DepAssetLossResultDto> results = tbAssetInventoryService.inventoryDepAssetById(depId);
//        return success(results);
//    }
//
//    @PostMapping("/exportDepAssetLossTb/{deptId}")
//    public void exportDepAssetLossTb(HttpServletResponse response, @PathVariable("deptId") String depId){
//        List<DepAssetLossResultDto> results = tbAssetInventoryService.inventoryDepAssetById(depId);
//        ExcelUtil<DepAssetLossResultDto> util = new ExcelUtil<DepAssetLossResultDto>(DepAssetLossResultDto.class);
//        util.exportExcel(response, results, "近两次资产盘点损耗统计");
//    }
}
