package com.dcsoft.system.face.controller;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.dcsoft.system.face.domain.FaceStudent;
import com.dcsoft.system.face.service.IFaceStudentService;
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.dcsoft.common.log.annotation.Log;
import com.dcsoft.common.log.enums.BusinessType;
import com.dcsoft.common.security.annotation.RequiresPermissions;
import com.dcsoft.system.face.domain.FaceImage;
import com.dcsoft.system.face.service.IFaceImageService;
import com.dcsoft.common.core.web.controller.BaseController;
import com.dcsoft.common.core.web.domain.AjaxResult;
import com.dcsoft.common.core.utils.poi.ExcelUtil;
import com.dcsoft.common.core.web.page.TableDataInfo;

/**
 * 图片信息Controller
 *
 * @author nichun
 * @date 2023-03-30
 */
@RestController
@RequestMapping("/image")
public class FaceImageController extends BaseController
{
    @Autowired
    private IFaceImageService faceImageService;

    @Autowired
    private IFaceStudentService faceStudentService;

    /**
     * 查询图片信息列表
     */
    @RequiresPermissions("face:image:list")
    @GetMapping("/list")
    public TableDataInfo list(FaceImage faceImage)
    {
        startPage();
        List<FaceImage> list = faceImageService.selectFaceImageList(faceImage);
        return getDataTable(list);
    }

    /**
     * 查询图片信息列表
     */
    @GetMapping("/imageList")
    public List<FaceImage> imageList(FaceImage faceImage)
    {
        List<FaceImage> list = faceImageService.selectFaceImageList(faceImage);
        return list;
    }

    /**
     * 导出图片信息列表
     */
    @RequiresPermissions("face:image:export")
    @Log(title = "图片信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export1")
    public void export1(HttpServletResponse response, FaceImage faceImage)
    {
        List<FaceImage> list = faceImageService.selectFaceImageList(faceImage);
        ExcelUtil<FaceImage> util = new ExcelUtil<FaceImage>(FaceImage.class);
        util.exportExcel(response, list, "图片信息数据");
    }

    /**
     * 导出图片压缩包
     */
    @RequiresPermissions("face:image:export")
    @Log(title = "图片信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FaceImage faceImage)
    {
        List<FaceImage> list = faceImageService.selectFaceImageList(faceImage);
        ZipOutputStream zipOutputStream = null;
        try {
            zipOutputStream = new ZipOutputStream(response.getOutputStream());
            for (FaceImage image: list) {
                String pictureUrl=image.getImgs();
                String fileName = pictureUrl.substring(pictureUrl.lastIndexOf("/") + 1);
                zipOutputStream.putNextEntry(new ZipEntry(fileName));

                URL url = new URL(pictureUrl);
                InputStream inputStream = new DataInputStream(url.openStream());

                byte[] buff = new byte[1024];
                int len;
                while ((len = inputStream.read(buff)) != -1) {
                    zipOutputStream.write(buff, 0, len);
                }
                zipOutputStream.closeEntry();
                inputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (zipOutputStream != null) {
                try {
                    zipOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * 获取图片信息详细信息
     */
    @RequiresPermissions("face:image:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(faceImageService.selectFaceImageById(id));
    }

    /**
     * 新增图片信息
     */
    @RequiresPermissions("face:image:add")
    @Log(title = "图片信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FaceImage faceImage)
    {
        //如果是主照片，将主照片信息同步到学生信息表中
        if("0".equals(faceImage.getLabel())){
            FaceStudent   faceStudent=new FaceStudent();
            faceStudent.setStuId(faceImage.getStuId());
            List<FaceStudent> faceStudentList= faceStudentService.selectFaceStudentList(faceStudent);
            if(faceStudentList.size()>0){
                FaceStudent faceStudents=faceStudentList.get(0);
                if(!faceImage.getImgs().equals(faceStudents.getImgs())){
                    faceStudents.setImgs(faceImage.getImgs());
                    faceStudentService.updateFaceStudent(faceStudents);
                }
            }
        }
        return toAjax(faceImageService.insertFaceImage(faceImage));
    }

    /**
     * 新增图片信息
     */
    @Log(title = "图片信息", businessType = BusinessType.INSERT)
    @PostMapping("/addImage")
    public AjaxResult addImage(@RequestBody FaceImage faceImage)
    {
        //如果是主照片，将主照片信息同步到学生信息表中
        if("0".equals(faceImage.getLabel())){
            FaceStudent   faceStudent=new FaceStudent();
            faceStudent.setStuId(faceImage.getStuId());
            List<FaceStudent> faceStudentList= faceStudentService.selectFaceStudentList(faceStudent);
            if(faceStudentList.size()>0){
                FaceStudent faceStudents=faceStudentList.get(0);
                if(!faceImage.getImgs().equals(faceStudents.getImgs())){
                    faceStudents.setImgs(faceImage.getImgs());
                    faceStudentService.updateFaceStudent(faceStudents);
                }
            }
        }
        return toAjax(faceImageService.insertFaceImage(faceImage));
    }

    /**
     * 修改图片信息
     */
    @Log(title = "图片信息", businessType = BusinessType.UPDATE)
    @PutMapping("/editImage")
    public AjaxResult editImage(@RequestBody FaceImage faceImage)
    {
        //如果是主照片，将主照片信息同步到学生信息表中
        if("0".equals(faceImage.getLabel())){
            FaceStudent   faceStudent=new FaceStudent();
            faceStudent.setStuId(faceImage.getStuId());
            List<FaceStudent> faceStudentList= faceStudentService.selectFaceStudentList(faceStudent);
            if(faceStudentList.size()>0){
                FaceStudent faceStudents=faceStudentList.get(0);
                if(!faceImage.getImgs().equals(faceStudents.getImgs())){
                    faceStudents.setImgs(faceImage.getImgs());
                    faceStudentService.updateFaceStudent(faceStudents);
                }
            }
        }
        return toAjax(faceImageService.updateFaceImage(faceImage));
    }


    /**
     * 修改图片信息
     */
    @RequiresPermissions("face:image:edit")
    @Log(title = "图片信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FaceImage faceImage)
    {
        //如果是主照片，将主照片信息同步到学生信息表中
        if("0".equals(faceImage.getLabel())){
        FaceStudent   faceStudent=new FaceStudent();
        faceStudent.setStuId(faceImage.getStuId());
        List<FaceStudent> faceStudentList= faceStudentService.selectFaceStudentList(faceStudent);
            if(faceStudentList.size()>0){
                FaceStudent faceStudents=faceStudentList.get(0);
                if(!faceImage.getImgs().equals(faceStudents.getImgs())){
                    faceStudents.setImgs(faceImage.getImgs());
                    faceStudentService.updateFaceStudent(faceStudents);
                }
            }
        }
        return toAjax(faceImageService.updateFaceImage(faceImage));
    }

    /**
     * 删除图片信息
     */
    @RequiresPermissions("face:image:remove")
    @Log(title = "图片信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(faceImageService.deleteFaceImageByIds(ids));
    }

    /**
     * 删除图片信息
     */
    @Log(title = "图片信息", businessType = BusinessType.DELETE)
    @GetMapping("/delImage/{ids}")
    public AjaxResult delImage(@PathVariable Long[] ids)
    {
        return toAjax(faceImageService.deleteFaceImageByIds(ids));
    }


}
