package com.zd.chemical.controller;

import java.util.Collections;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import javax.servlet.http.HttpServletResponse;

import com.zd.chemical.domain.vo.*;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.web.domain.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.chemical.domain.ActHxpapply;
import com.zd.chemical.service.IActHxpapplyService;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.zd.common.core.web.page.TableDataInfo;

/**
 * 化学品申购Controller
 *
 * @author zd
 * @date 2022-08-17
 */
@RestController
@Api(tags = "【化学品申购】")
@RequestMapping("/hxpapply")
public class ActHxpapplyController extends BaseController<ActHxpapply>
{
    @Autowired
    private IActHxpapplyService actHxpapplyService;

    /**
     * 查询化学品申购列表
     */
    @PreAuthorize(hasPermi = PerPrefix.ACT_HXP_APPLY+ PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询化学品申购列表")
    public TableDataInfo<ActHxpapplyListVo> list(ActHxpapplySearch actHxpapplySearch)
    {
        startPage();
        List<ActHxpapplyListVo> list = actHxpapplyService.selectActHxpapplyList(actHxpapplySearch);
        TableDataInfo<ActHxpapplyListVo> info=new TableDataInfo<>();
        info.getDataTable(list);
        return info;
    }


    /**
     * 查询化学品申购列表(草稿箱)
     */
    @PreAuthorize(hasPermi = PerPrefix.ACT_HXP_APPLY+ PerFun.DRAFTSLIST)
    @GetMapping("/draftsList")
    @ApiOperation(value = "查询化学品申购列表(草稿箱)")
    public TableDataInfo<ActHxpapplyDraftsListVo> draftsList(ActHxpapplySearch actHxpapplySearch)
    {
        actHxpapplySearch.setUserId(SecurityUtils.getUserId());
        startPage();
        List<ActHxpapplyDraftsListVo> list = actHxpapplyService.draftsList(actHxpapplySearch);
        TableDataInfo<ActHxpapplyDraftsListVo> info=new TableDataInfo<>();
        info.getDataTable(list);
        return info;
    }


    /**
     * 查询化学品申购列表(学生端接口)
     */
    @GetMapping("/myList")
    @ApiOperation(value = "查询化学品申购列表")
    public TableDataInfo<ActHxpapplyListVo> mylist(ActHxpapplySearch actHxpapplySearch)
    {
        startPage();
        actHxpapplySearch.setUserId(SecurityUtils.getUserId());
        List<ActHxpapplyListVo> list = actHxpapplyService.selectActHxpapplyList(actHxpapplySearch);
        TableDataInfo<ActHxpapplyListVo> info=new TableDataInfo<>();
        info.getDataTable(list);
        return info;
    }


    /**
     * 查询化学品申购列表(学生端草稿箱)
     */
    @GetMapping("/myDraftsList")
    @ApiOperation(value = "查询化学品申购列表(草稿箱)")
    public TableDataInfo<ActHxpapplyDraftsListVo> myDraftsList(ActHxpapplySearch actHxpapplySearch)
    {
        startPage();
        actHxpapplySearch.setUserId(SecurityUtils.getUserId());
        List<ActHxpapplyDraftsListVo> list = actHxpapplyService.draftsList(actHxpapplySearch);
        TableDataInfo<ActHxpapplyDraftsListVo> info=new TableDataInfo<>();
        info.getDataTable(list);
        return info;
    }

    /**
     * 导出化学品申购列表
     */
    @ApiOperation(value = "导出化学品申购列表")
    @PreAuthorize(hasPermi = PerPrefix.ACT_HXP_APPLY+ PerFun.EXPORT)
    @Log(title = "化学品申购", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ActHxpapplySearch actHxpapplySearch) throws IOException
    {
        List<ActHxpapplyListVo> list = actHxpapplyService.selectActHxpapplyList(actHxpapplySearch);
        Optional.ofNullable(list).orElseGet(Collections::emptyList)
                .stream()
                .forEach(a->{
                    StringBuffer currentApprover = new StringBuffer();
                    Optional.ofNullable(a.getCurrentUserList()).orElseGet(Collections::emptyList)
                            .stream()
                            .forEach(b->{
                                currentApprover.append(",");
                                currentApprover.append(b.get("name"));
                            });
                    if(currentApprover.length()>0){
                        a.setCurrentApprover(currentApprover.substring(1));
                    }

                    StringBuffer historyApprover = new StringBuffer();
                    Optional.ofNullable(a.getHistoryUserList()).orElseGet(Collections::emptyList)
                            .stream()
                            .forEach(b->{
                                historyApprover.append(",");
                                historyApprover.append(b.get("name"));
                            });
                    if(historyApprover.length()>0){
                        a.setHistoryApprover(historyApprover.substring(1));
                    }
                });
        ExcelUtil<ActHxpapplyListVo> util = new ExcelUtil<ActHxpapplyListVo>(ActHxpapplyListVo.class);
        util.exportExcel(response, list, "化学品申购数据");
    }

    /**
     * 获取化学品申购详细信息
     */
    @ApiOperation(value = "获取化学品申购详细信息")
    @PreAuthorize(hasPermi = PerPrefix.ACT_HXP_APPLY+ PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public ResultData<ActHxpapplyInfoVo> getInfo(@PathVariable("id") Long id)
    {
        return ResultData.success(actHxpapplyService.selectActHxpapplyById(id,2));
    }

    /**
     * 获取化学品申购详细信息(审批记录调用)
     */
    @ApiOperation(value = "获取化学品申购详细信息")
    @PreAuthorize(hasPermi = PerPrefix.ACT_HXP_APPLYAUDIT+ PerFun.QUERY)
    @GetMapping(value = "/audit/{id}")
    public ResultData<ActHxpapplyInfoVo> auditByGetInfo(@PathVariable("id") Long id)
    {
        return ResultData.success(actHxpapplyService.selectActHxpapplyById(id,1));
    }

    /**
     * 获取化学品申购详细信息(学生端接口)
     */
    @ApiOperation(value = "获取化学品申购详细信息(学生端接口)")
    @GetMapping(value = "/myInfo/{id}")
    public ResultData<ActHxpapplyInfoVo> getMyInfo(@PathVariable("id") Long id)
    {
        return ResultData.success(actHxpapplyService.selectActHxpapplyById(id,2));
    }


    /**
     * 新增化学品申购
     */
    @ApiOperation(value = "新增化学品申购")
    @PreAuthorize(hasPermi = PerPrefix.ACT_HXP_APPLY+ PerFun.ADD)
    @Log(title = "化学品申购", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody ActHxpapplyVo actHxpapplyVo)
    {
        return ResultData.result(actHxpapplyService.insertActHxpapply(actHxpapplyVo));
    }

    /**
     * 新增化学品申购(学生端接口)
     */
    @ApiOperation(value = "新增化学品申购(学生端接口)")
    @Log(title = "化学品申购", businessType = BusinessType.INSERT)
    @PostMapping("/myadd")
    public ResultData myadd(@RequestBody ActHxpapplyVo actHxpapplyVo)
    {
        return ResultData.result(actHxpapplyService.insertActHxpapply(actHxpapplyVo));
    }

    /**
     * 修改化学品申购
     */
    @ApiOperation(value = "修改化学品申购")
//    @PreAuthorize(hasPermi = "laboratory:hxpapply:edit")
    @Log(title = "化学品申购", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData edit(@RequestBody ActHxpapply actHxpapply)
    {
        return  ResultData.result(actHxpapplyService.updateActHxpapply(actHxpapply));
    }

    /**
     * 删除化学品申购
     */
    @ApiOperation(value = "删除化学品申购")
    @PreAuthorize(hasPermi = PerPrefix.ACT_HXP_APPLY+ PerFun.REMOVE)
    @Log(title = "化学品申购", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable Long[] ids)
    {
        return ResultData.result(actHxpapplyService.deleteActHxpapplyByIds(ids));
    }


    /**
     * 删除化学品申购(学生端接口)
     */
    @ApiOperation(value = "删除化学品申购(学生端接口)")
    @Log(title = "化学品申购", businessType = BusinessType.DELETE)
    @DeleteMapping("/myDel/{ids}")
    public ResultData myDel(@PathVariable Long[] ids)
    {
        return ResultData.result(actHxpapplyService.deleteActHxpapplyByIds(ids));
    }


    /**
     * 获取化学品剩余可存放量
     */
    @ApiOperation(value = "获取化学品剩余可存放量")
    @GetMapping(value = "/getRestStock")
    public ResultData<Map <String, String>> getRestStock(ActRestStockVo actRestStockVo)
    {
        return ResultData.success(actHxpapplyService.getRestStock(actRestStockVo));
    }


    /**
     * 获取化学品剩余可存放量（化学品存放用，一个化学品多个机柜用）
     */
    @ApiOperation(value = "获取化学品剩余可存放量")
    @GetMapping(value = "/getRestStockByManyCabinet")
    public ResultData<List<ActRestStockManyVo>> getRestStockByManyCabinet(ActRestStockManySearch actRestStockVo)
    {
        return ResultData.success(actHxpapplyService.getRestStockByManyCabinet(actRestStockVo));
    }
}
