package com.emr.controller.emrPdfWaterSet;

import com.emr.dao.emrPdfWaterSet.EmrPdfWaterSetMapper;
import com.emr.entity.ResultUtil;
import com.emr.entity.emrPdfWaterSet.EmrPdfWaterSet;
import com.emr.service.emrPdfWaterSet.PdfWaterSetService;
import com.emr.util.ExceptionPrintUtil;
import com.emr.vo.PdfWaterSetVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

@Controller
@RequestMapping("pdfWaterSet/")
public class PdfWaterSetController {
    @Value("${picPath}")
    private String picPath;
    @Autowired
    private EmrPdfWaterSetMapper pdfWaterSetMapper;
    @Autowired
    private PdfWaterSetService pdfWaterSetService;
    @RequiresPermissions("/pdfWaterSet/select")
    @RequestMapping("pdfWaterSetList")
    public String pdfWaterSetList(Model model){
        EmrPdfWaterSet pdfWaterSet = null;
        try {
            pdfWaterSet = pdfWaterSetMapper.selectByPrimaryKey(1);
        } catch (Exception e) {
            ExceptionPrintUtil.printException(e);
            e.printStackTrace();
        }
        PdfWaterSetVo vo = new PdfWaterSetVo();
        assert pdfWaterSet != null;
        BeanUtils.copyProperties(pdfWaterSet,vo);
        model.addAttribute("pdfWaterSet",vo);
        return "otherManage/pdfWaterSetList";
    }

    @RequiresPermissions("/pdfWaterSet/update")
    @RequestMapping(value="update",method= {RequestMethod.POST})
    @ResponseBody
    public ResultUtil update(@RequestParam(required = false,value = "files")
                                         MultipartFile files,EmrPdfWaterSet pdfWaterSet) throws Exception{
        //判断是否有选择图片
        if(null != files){
            EmrPdfWaterSet obj = pdfWaterSetMapper.selectByPrimaryKey(1);
            String imgFile = obj.getImgFile();
            //删除原有图片，上传现在图片
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String realPath = request.getSession().
                    getServletContext().getRealPath("/") + picPath;
            String src = realPath + imgFile;
            File file = new File(src);
            if(file.isFile()){
                file.delete();
            }
            String newSrc = realPath + pdfWaterSet.getImgFile();
            files.transferTo(new File(newSrc));
        }
        pdfWaterSetService.updatePdfWaterSet(pdfWaterSet);
        return ResultUtil.ok();
    }
}
