package com.zd.laboratory.controller;

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

import com.zd.common.core.constant.Constants;
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.security.utils.DictUtils;
import com.zd.laboratory.domain.LabSubject;
import com.zd.laboratory.domain.vo.LabSubjectAccessRecordVo;
import com.zd.laboratory.service.ILabSubjectAccessRecordService;
import com.zd.laboratory.service.ILabSubjectService;
import com.zd.system.api.domain.SysDictData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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.laboratory.domain.LabPhotoInspection;
import com.zd.laboratory.service.ILabPhotoInspectionService;
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 com.zd.common.core.web.page.TableDataInfo;

/**
 * 拍照检查附件Controller
 *
 * @author zd
 * @date 2021-08-31
 */
@RestController
@RequestMapping("/inspection")
public class LabPhotoInspectionController extends BaseController {
    @Autowired
    private ILabPhotoInspectionService labPhotoInspectionService;
    @Autowired
    private ILabSubjectAccessRecordService labSubjectAccessRecordService;
    @Autowired
    private ILabSubjectService subjectService;

    /**
     * 查询拍照检查附件列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_INSPECTION + PerFun.LIST)
    @GetMapping("/list")
    public TableDataInfo list(LabPhotoInspection labPhotoInspection) {
        startPage();
        List<LabPhotoInspection> list = labPhotoInspectionService.selectLabPhotoInspectionList(labPhotoInspection);
        return getDataTable(list);
    }

    /**
     * 导出拍照检查附件列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_INSPECTION + PerFun.EXPORT)
    @Log(title = "拍照检查附件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LabPhotoInspection labPhotoInspection) throws IOException {
        List<LabPhotoInspection> list = labPhotoInspectionService.selectLabPhotoInspectionList(labPhotoInspection);
        ExcelUtil<LabPhotoInspection> util = new ExcelUtil<LabPhotoInspection>(LabPhotoInspection.class);
        util.exportExcel(response, list, "拍照检查附件数据");
    }



    /**
     * 新增拍照检查附件
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_INSPECTION + PerFun.ADD)
    @Log(title = "拍照检查附件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LabPhotoInspection labPhotoInspection) {
        return toAjax(labPhotoInspectionService.insertLabPhotoInspection(labPhotoInspection));
    }

    /**
     * 修改拍照检查附件
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_INSPECTION + PerFun.EDIT)
    @Log(title = "拍照检查附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LabPhotoInspection labPhotoInspection) {
        return toAjax(labPhotoInspectionService.updateLabPhotoInspection(labPhotoInspection));
    }

    /**
     * 删除拍照检查附件
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_INSPECTION + PerFun.REMOVE)
    @Log(title = "拍照检查附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(labPhotoInspectionService.deleteLabPhotoInspectionByIds(ids));
    }

    /**
     * 获取拍照检查附件详细信息
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_INSPECTION + PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(labPhotoInspectionService.selectLabPhotoInspectionById(id));
    }



    /**
     * 获取离开实验室拍照附件列表
     */
    @RequestMapping(value = "/outSubjectPhoto")
    public AjaxResult outSubjectPhoto(LabSubjectAccessRecordVo accessRecordVo) {
        LabSubjectAccessRecordVo recordVo = labSubjectAccessRecordService.isSigInAndGet(SecurityUtils.getUserId(),accessRecordVo.getSubjectId());
        Map <String,Object> allMap = new HashMap <>();
        if(recordVo!=null){
            LabSubject subject = subjectService.selectLabSubjectById(recordVo.getSubjectId());
            if(subject!=null){
                String[] checkOut = subject.getCheckOut().split(",");

                allMap.put("accessId",recordVo.getId());
                List<Map <String,Object>> allList = new ArrayList <>();
                List<SysDictData> dictDataList = DictUtils.getDictCache("lab_onepc_outcheck");
                for(SysDictData dict:dictDataList){
                    for(int i=0;i<checkOut.length;i++){
                        if(dict.getDictValue().equals(checkOut[i])){
                            Map<String,Object> dictMap = new HashMap <>();
                            dictMap.put("dictLabel",dict.getDictLabel());
                            dictMap.put("dictValue",dict.getDictValue());
                            allList.add(dictMap);
                        }
                    }

                }
                allMap.put("photoList",allList);
            }
        }
        return AjaxResult.success(allMap);
//        return AjaxResult.success(labPhotoInspectionService.outSubjectPhoto());
    }



    /**
     * 获取离开实验室拍照附件列表
     */
    @RequestMapping(value = "/outSubjectList")
    public AjaxResult outSubjectList() {
        List<LabSubjectAccessRecordVo> recordVo = labSubjectAccessRecordService.isSigInAndGetList(SecurityUtils.getUserId());
        return AjaxResult.success(recordVo);
    }


    /**
     * 上面都不用了
     */

    /**
     * 获取拍照检查附件详细信息
     */
    @GetMapping()
    public AjaxResult getMyInfo() {
        LabSubjectAccessRecordVo sigInAndGet = labSubjectAccessRecordService.isSigInAndGet(SecurityUtils.getUserIdEx());
        if(sigInAndGet==null||sigInAndGet.getAccessStatus()!=1L)
        {
            return AjaxResult.success("您当前无正在进行的打卡记录!",false);
        }
        LabPhotoInspection labPhotoInspection = labPhotoInspectionService.selectLabPhotoInspectionById(sigInAndGet.getId());
        return AjaxResult.success(labPhotoInspection);
    }



    /**
     * 小程序新增&修改拍照检查附件
     */
    @Log(title = "小程序新增&修改拍照检查附件", businessType = BusinessType.INSERT)
    @ApiOperation(value = "小程序新增&修改拍照检查附件")
    @PostMapping("/savaPhotoFj")
    public AjaxResult savaPhotoFj(@RequestBody @Validated LabPhotoInspection labPhotoInspectionList) {
        return toAjax(labPhotoInspectionService.savaPhotoFj(labPhotoInspectionList));
    }

}
