package com.zd.airbottle.controller;

import com.zd.airbottle.domain.QpBottleStorage;
import com.zd.airbottle.domain.QpTask;
import com.zd.airbottle.domain.vo.*;
import com.zd.airbottle.service.*;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.poi.ExcelUtil;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.common.security.service.TokenService;
import com.zd.system.api.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 待办清单表Controller
 *
 * @author zd
 * @date 2022-05-11
 */
@RestController
@Api(tags = "【待办清单表-管理端】")
//作者手动加入 ，在接口上加作者@ApiOperationSupport(author = "XXX")
//@ApiSupport(author = Knife4jConfiguration.Author.)
    @RequestMapping("/taskManage")
public class QpTaskManageController extends BaseController
{
    @Autowired
    private IQpTaskService qpTaskService;

    @Autowired
    private IQpTaskDetailService qpTaskDetailService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IQpAirGoodsConfigService qpAirGoodsConfigService;

    @Autowired
    private IQpUseRecordService qpUseRecordService;

    @Autowired
    private IQpBottleStorageService qpBottleStorageService;


    /**
     * 查询待办清单表列表
     */
    // @PreAuthorize(hasPermi = "airbottle:task:list")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询待办清单表列表",notes = "权限字符："+ PerPrefix.AIRBOTTLE_TASK+ PerFun.LIST)
    public TableDataInfo<QpTaskVO> list(QpTaskVO qpTask)
    {
        if("login".equals(qpTask.getRemark())){
            //获取当前登录用户信息
            SysUser sysUser = tokenService.getLoginUser().getSysUser();
            qpTask.setUserId(sysUser.getUserId());//当前登陆人申请数据
        }

        startPage();
        List<QpTaskVO> list = qpTaskService.selectQpTaskList(qpTask);
        if(!list.isEmpty()){
            for (QpTaskVO qpTaskVO : list) {
                List<QpTaskDetailVO> detailList = qpTaskDetailService.selectQpTaskDetailByIdRelation(qpTaskVO.getId());
                qpTaskVO.setDetailListVO(detailList);
            }
        }
        return getDataTable(list);
    }


    /****
     * 查询供应商的货物配置信息
     * @param relationVo
     * @return
     */
    @GetMapping("/listGood")
    @ApiOperation(value = "查询供应商的货物配置信息")
    public TableDataInfo<QpAirGoodsConfigRelationVo> listGood(QpAirGoodsConfigRelationVo relationVo)
    {
        startPage();
        List<QpAirGoodsConfigRelationVo> list = qpAirGoodsConfigService.selectQpAirGoodsConfigRelationList(relationVo);
        return getDataTable(list);
    }


    /**
     * 导出待办清单表列表
     */
    @ApiOperation(value = "导出待办清单表列表",notes = "权限字符："+ PerPrefix.AIRBOTTLE_TASK+ PerFun.EXPORT)
    // @PreAuthorize(hasPermi = "airbottle:task:export")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ PerFun.EXPORT)
    @Log(title = "待办清单表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,QpTaskVO qpTask) throws IOException
    {
        if("login".equals(qpTask.getRemark())){
            //获取当前登录用户信息
            SysUser sysUser = tokenService.getLoginUser().getSysUser();
            qpTask.setUserId(sysUser.getUserId());//当前登陆人申请数据
        }

        List<QpTaskVO> list = qpTaskService.selectQpTaskList(qpTask);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<QpTaskExcelVo> listExcel = new ArrayList<>();
        for (QpTaskVO taskVO:list) {
            QpTaskExcelVo excelVo = new QpTaskExcelVo();
            excelVo.setUserName(taskVO.getUserName());//申领人
            excelVo.setPhone(taskVO.getPhone());//联系方式
            excelVo.setLocation(taskVO.getLocation());//实验地点
            excelVo.setStrTime(sdf.format(taskVO.getCreateTime()));//申领时间
            listExcel.add(excelVo);
        }
        ExcelUtil<QpTaskExcelVo> util = new ExcelUtil<>(QpTaskExcelVo.class);
        //本地日期,不包括时分秒
        LocalDate nowDate = LocalDate.now();
        util.exportExcel(response, listExcel, "申领记录数据", null, "申领记录信息数据导出表"+nowDate);
    }

    /**
     * 获取待办清单表详细信息
     */
    @ApiOperation(value = "根据待办清单id获取待办清单详情",notes = "权限字符："+ PerPrefix.AIRBOTTLE_TASK+ PerFun.QUERY)
    // @PreAuthorize(hasPermi = "airbottle:task:query")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Map<String,Object> map = new HashMap<String,Object>();


        //待办清单信息
        QpTask qpTask = qpTaskService.selectQpTaskById(id);
        map.put("qpTask",qpTask);

        //待办清单详情信息
        List<QpTaskDetailVO> detailList = qpTaskDetailService.selectQpTaskDetailByIdRelation(id);
        map.put("detailList",detailList);

        return AjaxResult.success(map);
    }

    /**
     * 管理端根据taskID查询任务清单
     */
    @ApiOperation(value = "管理端根据taskID查询任务清单",notes = "权限字符："+ PerPrefix.AIRBOTTLE_TASK+ PerFun.QUERY)
    // @PreAuthorize(hasPermi = "airbottle:task:query")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ PerFun.QUERY)
    @GetMapping(value = "/detail/{taskId}")
    public AjaxResult getDetailsInfo(@PathVariable("taskId") Long taskId)
    {
        List<QpTaskDetailVO> detailVOS=qpTaskDetailService.getDetailsInfo(taskId);
        return AjaxResult.success(detailVOS);
    }

    /**
     * 新增待办清单表
     */
    @ApiOperation(value = "新增待办清单表",notes = "权限字符："+ PerPrefix.AIRBOTTLE_TASK+ PerFun.ADD)
    // @PreAuthorize(hasPermi = "airbottle:task:add")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ PerFun.ADD)
    @Log(title = "待办清单表", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody QpTaskVO qpTaskVO)
    {
        return ResultData.result(qpTaskService.insertQpTask(qpTaskVO));
    }

    /**
     * 修改待办清单表
     */
    @ApiOperation(value = "修改待办清单表",notes = "权限字符："+ PerPrefix.AIRBOTTLE_TASK+ PerFun.EDIT )
   // @PreAuthorize(hasPermi = "airbottle:task:edit")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ PerFun.EDIT)
    @Log(title = "待办清单表", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData edit(@RequestBody QpTaskVO qpTaskVo)
    {
        return  ResultData.result(qpTaskService.updateQpTask(qpTaskVo));
    }

    /**
     * 删除待办清单表
     */
    @ApiOperation(value = "删除待办清单表",notes = "权限字符："+ PerPrefix.AIRBOTTLE_TASK+ PerFun.REMOVE)
    // @PreAuthorize(hasPermi = "airbottle:task:remove")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ PerFun.REMOVE)
    @Log(title = "待办清单表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable Long[] ids)
    {
        return ResultData.result(qpTaskService.deleteQpTaskByIds(ids));
    }

    /**
     * 气瓶信息列表-管理端
     * @param airGoodsConfigRelationVo
     * @return
     */
    @GetMapping("/taskListDetail")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ "bottlelist")
    @ApiOperation(value = "气瓶信息列表-管理端")
    public TableDataInfo<QpAirGoodsConfigRelationVo> taskListDetail(QpAirGoodsConfigRelationVo airGoodsConfigRelationVo){
        //当前登陆人申请的已经入库的气瓶
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        startPage();
        airGoodsConfigRelationVo.setUserId(sysUser.getUserId());
        List<QpAirGoodsConfigRelationVo> relationVoList = qpTaskDetailService.selectQpTaskDetailRelationList(airGoodsConfigRelationVo);
        return getDataTable(relationVoList);
    }

    /**
     * 气瓶信息详情
     * @param id 入库id
     * @return
     */
    @ApiOperation(value = "气瓶信息详情",notes = "权限字符："+ PerPrefix.AIRBOTTLE_TASK+ "bottleDetails")
    // @PreAuthorize(hasPermi = "airbottle:task:query")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ PerFun.QUERY)
    @GetMapping(value = "/bottleDetails")
    public AjaxResult bottleDetails(Long id)
    {
        Map<String,Object> map = new HashMap<String,Object>();
        //查询入库信息
        QpBottleStorage bottleStorage = qpBottleStorageService.selectQpBottleStorageById(id);

        //待办清单信息
        QpTask qpTask = qpTaskService.selectQpTaskById(bottleStorage.getTaskId());
        map.put("qpTask",qpTask);


        //使用记录详情
        QpUseRecordVo qpUseRecordVo = new QpUseRecordVo();
        qpUseRecordVo.setStorageId(id);//入库表id
        List<QpUseRecordVo> list =  qpUseRecordService.getUseDetailsListByStorageId(qpUseRecordVo);
        map.put("list",list);
        return AjaxResult.success(map);
    }
}
