package com.ruoyi.asset.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.asset.domain.TbLocation;
import com.ruoyi.asset.domain.dto.PrintDTO;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.label.domain.TbLabelModelInfo;
import com.ruoyi.label.service.ITbLabelModelInfoService;
import com.ruoyi.ptxlib.domain.PTXLabelCode;
import com.ruoyi.ptxlib.domain.PTXLabelInfo;
import com.ruoyi.ptxlib.domain.PTXLabelText;
import com.ruoyi.ptxlib.service.impl.PTXService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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.asset.domain.TbAssetInformation;
import com.ruoyi.asset.service.ITbAssetInformationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 资产信息Controller
 * 
 * @author 原动力
 * @date 2023-05-15
 */
@RestController
@RequestMapping("/asset/information")
public class TbAssetInformationController extends BaseController
{
    @Autowired
    private ITbAssetInformationService tbAssetInformationService;

    /**
     * 查询资产信息列表
     */
    @PreAuthorize("@ss.hasPermi('asset:information:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbAssetInformation tbAssetInformation)
    {
        List<TbAssetInformation> list = tbAssetInformationService.selectTbAssetInformationList(tbAssetInformation);
        return getDataTable(list);
    }

    /**
     * 导出资产信息列表
     */
    @PreAuthorize("@ss.hasPermi('asset:information:export')")
    @Log(title = "资产信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbAssetInformation tbAssetInformation)
    {
        List<TbAssetInformation> list = tbAssetInformationService.selectTbAssetInformationList(tbAssetInformation);
        ExcelUtil<TbAssetInformation> util = new ExcelUtil<TbAssetInformation>(TbAssetInformation.class);
        util.exportExcel(response, list, "资产信息数据");
    }

    /**
     * 导出资产信息列表
     */
    @PreAuthorize("@ss.hasPermi('asset:information:export')")
    @Log(title = "资产信息", businessType = BusinessType.EXPORT)
    @PostMapping("/allExport")
    public void allExport(HttpServletResponse response)
    {
        List<TbAssetInformation> list = tbAssetInformationService.selectTbAssetInformationList2(new TbAssetInformation());
        ExcelUtil<TbAssetInformation> util = new ExcelUtil<TbAssetInformation>(TbAssetInformation.class);
        util.exportExcel(response, list, "资产信息数据");
    }

    /**
     * 获取资产信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('asset:information:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbAssetInformationService.selectTbAssetInformationById(id));
    }

    @Resource
    private PTXService ptxService;

    @Resource
    private ITbLabelModelInfoService labelModelInfoService;

    @PostMapping("/print")
    public AjaxResult print(@RequestBody PrintDTO printDTO) {
        List<Long> ids = printDTO.getIds();
        List<TbAssetInformation> tbAssetInformationList = tbAssetInformationService.selectTbAssetInformationByIds(ids);
        if (tbAssetInformationList.isEmpty()) {
            return AjaxResult.error("资产信息不存在");
        }

        String labelCode = printDTO.getLabelCode();
        TbLabelModelInfo tbLabelModelInfo = labelModelInfoService.selectTbLabelModelByLabelCode(labelCode);
        if (tbLabelModelInfo == null) {
            return AjaxResult.error("标签模板不存在");
        }

        /*
        * Java赋值为引用赋值，更改赋值对象数据，也会影响到原对象！
        * 解决方案一：深度拷贝
        * 解决方案二：将原对象转为json字符串，用到时转回来给新的对象赋值*/

        String ptxLabelInfoModel = tbLabelModelInfo.getLabelValue();

        List<PTXLabelInfo> ptxLabelInfos = new ArrayList<>();
        for (TbAssetInformation tbAssetInformation : tbAssetInformationList) {
            // 然后再转回对象，便不会影响到原对象
            PTXLabelInfo ptxLabelInfo = JSONUtil.toBean(ptxLabelInfoModel, PTXLabelInfo.class);
            ptxLabelInfo = ptxService.filterInfo(ptxLabelInfo, tbAssetInformation);
            ptxLabelInfos.add(ptxLabelInfo);
        }

        for (PTXLabelInfo ptxLabelInfo : ptxLabelInfos) {
            ptxService.printLabel(ptxLabelInfo);
        }

        return AjaxResult.success(ptxLabelInfos);
    }

    /**
     * 新增资产信息
     */
    @PreAuthorize("@ss.hasPermi('asset:information:add')")
    @Log(title = "资产信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbAssetInformation tbAssetInformation)
    {
        String number = tbAssetInformation.getNumber();
        tbAssetInformation.setCode(number);
        return toAjax(tbAssetInformationService.insertTbAssetInformation(tbAssetInformation));
    }

    /**
     * 修改资产信息
     */
    @PreAuthorize("@ss.hasPermi('asset:information:edit')")
    @Log(title = "资产信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbAssetInformation tbAssetInformation)
    {
        String number = tbAssetInformation.getNumber();
        tbAssetInformation.setCode(number);
        return toAjax(tbAssetInformationService.updateTbAssetInformation(tbAssetInformation));
    }

    /**
     * 删除资产信息
     */
    @PreAuthorize("@ss.hasPermi('asset:information:remove')")
    @Log(title = "资产信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbAssetInformationService.deleteTbAssetInformationByIds(ids));
    }

    /**
     * 自定义字段
     */
    @PreAuthorize("@ss.hasPermi('asset:information:query')")
    @Log(title = "资产信息字段自定义设置")
    @PostMapping("/setDiy")
    public AjaxResult setDiy(@RequestBody List<String> labels)
    {
        return toAjax(tbAssetInformationService.setDiy(labels));
    }

    /**
     * 自定义字段
     */
    @PreAuthorize("@ss.hasPermi('asset:information:query')")
    @Log(title = "资产信息字段自定义获取")
    @GetMapping("/getDiy")
    public AjaxResult getDiy()
    {
        return success(tbAssetInformationService.getDiy());
    }

    /**
     * 导入excel
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "资产信息导入", businessType = BusinessType.IMPORT) // todo
    @PreAuthorize("@ss.hasPermi('asset:information:import')") // todo
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TbAssetInformation> util = new ExcelUtil<>(TbAssetInformation.class); // todo
        List<TbAssetInformation> stuList = util.importExcel(file.getInputStream()); // todo
        String operName = getUsername();
        String message = tbAssetInformationService.importCon(stuList, updateSupport, operName); // todo
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TbAssetInformation> util = new ExcelUtil<>(TbAssetInformation.class); // todo
        util.importTemplateExcel(response, "资产信息");
    }
}
