package com.zd.laboratory.controller;

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

import com.zd.common.core.domain.R;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.utils.StringUtils;
import com.zd.common.core.valid.ValidGroup;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.security.service.TokenService;
import com.zd.laboratory.domain.LabMessageDynamic;
import com.zd.laboratory.domain.dto.LabPhotoNoteADD;
import com.zd.laboratory.domain.vo.LabPhotoNoteListVO;
import com.zd.laboratory.domain.vo.LabPhotoNoteVO;
import com.zd.laboratory.service.ILabMessageDynamicService;
import com.zd.laboratory.service.ILabMessageUserService;
import com.zd.system.api.domain.SysFile;
import com.zd.system.api.laboratory.domain.LabMessageUser;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.laboratory.domain.LabPhotoNote;
import com.zd.laboratory.service.ILabPhotoNoteService;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

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

/**
 * 随手拍记录Controller
 *
 * @author 周攀
 * @date 2021-09-10
 */
@RestController
@Api(tags = "【随手拍记录】")
@RequestMapping("/photonote")
public class LabPhotoNoteController extends BaseController {
    @Autowired
    private ILabPhotoNoteService labPhotoNoteService;


    /**
     * 查询随手拍记录列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_PHOTONOTE + PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询随手拍记录列表")
    public TableDataInfo<LabPhotoNoteListVO> list(LabPhotoNote labPhotoNote) {
        startPage();
        List<LabPhotoNoteListVO> list = labPhotoNoteService.selectLabPhotoNoteVOList(labPhotoNote);
        return getDataTable(list);
    }

//    /**
//     * 导出随手拍记录列表
//     */
//    @ApiOperation(value = "导出随手拍记录列表")
//    @PreAuthorize(hasPermi = "laboratory:photonote:export")
//    @Log(title = "随手拍记录", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, LabPhotoNote labPhotoNote) throws IOException
//    {
//        List<LabPhotoNote> list = labPhotoNoteService.selectLabPhotoNoteList(labPhotoNote);
//        ExcelUtil<LabPhotoNote> util = new ExcelUtil<LabPhotoNote>(LabPhotoNote.class);
//        util.exportExcel(response, list, "随手拍记录数据");
//    }

    /**
     * 获取随手拍记录详细信息
     */
    @ApiOperation(value = "获取随手拍记录详细信息")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_PHOTONOTE + PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public LabPhotoNoteListVO getInfo(@PathVariable("id") Long id) {
        LabPhotoNote labPhotoNote = new LabPhotoNote();
        labPhotoNote.setId(id);
        List<LabPhotoNoteListVO> list = labPhotoNoteService.selectLabPhotoNoteVOList(labPhotoNote);
        return Optional.ofNullable(list.get(0)).orElseGet(null);
    }

    /**
     * 新增随手拍记录
     */
    @ApiOperation(value = "新增随手拍记录")
    @Log(title = "随手拍记录", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData<Long> add(@RequestBody @Validated(ValidGroup.Crud.Create.class) LabPhotoNoteADD labPhotoNote) {
        return ResultData.result(labPhotoNoteService.saveLabPhotoNote(labPhotoNote));
    }

    /**
     * 我的随手拍记录
     */
    @ApiOperation(value = "我的随手拍记录")
    @Log(title = "随手拍记录", businessType = BusinessType.INSERT)
    @GetMapping("/my/list")
    public TableDataInfo<LabPhotoNote> myList() {
        LabPhotoNote labPhotoNote = new LabPhotoNote();
        labPhotoNote.setUserId(SecurityUtils.getUserId());
        startPage();
        List<LabPhotoNote> list = labPhotoNoteService.selectLabPhotoNoteList(labPhotoNote);
        return getDataTable(list);
    }

    /**
     * 处理随手拍记录
     */
    @ApiOperation(value = "处理随手拍记录")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_PHOTONOTE + PerFun.EDIT)
    @PostMapping("/handle")
    public ResultData handle(@RequestBody LabPhotoNote labPhotoNote) {
//        LabPhotoNote add = new LabPhotoNote();
//        add.setHandleStatus(1);
//        add.setHandleTime(DateUtils.getNowDate());
//        add.setId(labPhotoNote.getId());
        labPhotoNote.setHandleStatus(1);
        labPhotoNote.setHandleTime(DateUtils.getNowDate());
        labPhotoNote.setHandleUser(SecurityUtils.getUserId());
        return ResultData.result(labPhotoNoteService.updateLabPhotoNote(labPhotoNote));
    }

//    /**
//     * 修改随手拍记录
//     */
//    @ApiOperation(value = "修改随手拍记录")
//    @PreAuthorize(hasPermi = "laboratory:photonote:edit")
//    @Log(title = "随手拍记录", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public ResultData edit(@RequestBody LabPhotoNote labPhotoNote)
//    {
//        return  ResultData.result(labPhotoNoteService.updateLabPhotoNote(labPhotoNote));
//    }

//    /**
//     * 删除随手拍记录
//     */
//    @ApiOperation(value = "删除随手拍记录")
//    @PreAuthorize(hasPermi = "laboratory:photonote:remove")
//    @Log(title = "随手拍记录", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{ids}")
//    public ResultData remove(@PathVariable Long[] ids)
//    {
//        return ResultData.result(labPhotoNoteService.deleteLabPhotoNoteByIds(ids));
//    }

    /**
     * 查询登录人接收随手拍统计
     */
    @GetMapping("/receivePhotoNote")
    @ApiOperation(value = "查询随手拍记录列表")
    public AjaxResult receivePhotoNote(LabPhotoNote labPhotoNote) {
        labPhotoNote.setHandleUser(SecurityUtils.getUserId());
        LabPhotoNoteVO labPhotoNoteVO = labPhotoNoteService.receivePhotoNote(labPhotoNote);
        return AjaxResult.success(labPhotoNoteVO);
    }


    /**
     * 小程序查询随手拍记录列表
     */
    @RequestMapping("/getAppList")
    @ApiOperation(value = "小程序查询随手拍记录列表")
    public TableDataInfo<LabPhotoNoteListVO> getAppList(@RequestBody LabPhotoNoteVO labPhotoNote) {
        startPage(labPhotoNote.getAppPageNum(),labPhotoNote.getAppPageSize());
        List<LabPhotoNoteListVO> list = labPhotoNoteService.selectLabPhotoNoteVOList(labPhotoNote);
        return getDataTable(list);
    }



    /**
     * 小程序处理随手拍记录
     */
    @ApiOperation(value = "小程序处理随手拍记录")
    @RequestMapping("/appHandle")
    public ResultData appHandle(@RequestBody LabPhotoNote note) {
        LabPhotoNote add = new LabPhotoNote();
        add.setHandleStatus(1);
        add.setHandleTime(DateUtils.getNowDate());
        add.setId(note.getId());
        add.setHandleDescribe(note.getHandleDescribe());
        add.setHandleImg(note.getHandleImg());
        add.setHandleUser(SecurityUtils.getUserId());
        return ResultData.result(labPhotoNoteService.updateLabPhotoNote(add));
    }


    /**
     * 小程序查询登录人接收随手拍统计
     */
    @RequestMapping("/appReceivePhotoNote")
    @ApiOperation(value = "小程序查询随手拍记录列表")
    public AjaxResult appReceivePhotoNote(@RequestBody LabPhotoNote labPhotoNote) {
        labPhotoNote.setHandleUser(SecurityUtils.getUserId());
        LabPhotoNoteVO labPhotoNoteVO = labPhotoNoteService.receivePhotoNote(labPhotoNote);
        return AjaxResult.success(labPhotoNoteVO);
    }


    /**
     * 获取随手拍记录详细信息 （小程序专用）
     */
    @ApiOperation(value = "获取随手拍记录详细信息")
    @GetMapping(value = "/getPhotonote/{id}")
    public LabPhotoNoteListVO getPhotonote(@PathVariable("id") Long id) {
        LabPhotoNote labPhotoNote = new LabPhotoNote();
        labPhotoNote.setId(id);
        List<LabPhotoNoteListVO> list = labPhotoNoteService.selectLabPhotoNoteVOList(labPhotoNote);
        return Optional.ofNullable(list.get(0)).orElseGet(null);
    }
}
