package com.ssm.controller;

import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.core.utils.FtpUtils;
import com.ssm.manage.entity.StaffWorks;
import com.ssm.manage.query.StaffWorkRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmStaffWorksService;
import com.ssm.manage.utils.Constant;
import com.ssm.manage.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Api(value="员工作品controller",tags={"员工作品相关接口"})
@RestController
@RequestMapping("/demay/ssm/stWorks")
@Slf4j
public class SsmStaffWorksController {
    @Autowired
    private SsmStaffWorksService ssmStaffWorksService;

    @ApiOperation(value = "查询当前员工所有作品  门店端-员工作品-查询当前员工所有作品",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/list")
    public GenericResponse selectList(@RequestBody StaffWorkRequest staffWorkRequest){
        GenericResponse<StaffWorks> genericResponse = new GenericResponse<>();
        List<StaffWorks> list = new ArrayList<>();
        if (staffWorkRequest.getStaffId() == null){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"员工Id");
            return genericResponse;
        }
        try {
            list = ssmStaffWorksService.selectList(staffWorkRequest);
        }catch (Exception e){
            log.error("SsmStaffWorksController.list:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setRows(list);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "删除员工作品   门店端-员工作品-删除员工作品",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/delete")
    public GenericResponse delete(@RequestBody StaffWorkRequest staffWorkRequest){
        GenericResponse<StaffWorks> genericResponse = new GenericResponse<>();
        if (staffWorkRequest.getId() == null){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"作品Id");
            return genericResponse;
        }
        StaffWorks staffWorks = new StaffWorks();
        staffWorks.setId(staffWorkRequest.getId());
        StaffWorks works = ssmStaffWorksService.selectOne(staffWorks);
        String path = works.getPictureFilePath();
        String name = works.getPictureFile();
        try {
            new FtpUtils().deleteFile(path,name);
        } catch (IOException e) {
            log.error("SsmStaffWorksController.delete:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FILE_DELETE_FAILED);
            return genericResponse;
        }
//        File file = new File(path + "/" + name);
//        //删除文件
//        if(file.isFile() && file.exists()) {
//            file.delete();
//        }
        int i = 0;
        try {
            i = ssmStaffWorksService.deleteStaffWorks(staffWorkRequest);
        }catch (Exception e){
            log.error("SsmStaffWorksController.delete:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "添加员工作品  门店端-员工作品-添加员工作品",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/add")
    public GenericResponse add(MultipartFile multipartFile, StaffWorks staffWorks, HttpSession session){
        GenericResponse genericResponse = new GenericResponse();
        if(null == multipartFile){
            genericResponse.setCodeAndMsg(ResponseCode.FILE_ISNULL);
            return genericResponse;
        }
        if(null == staffWorks.getStaffId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"员工Id");
            return genericResponse;
        }
        String fileName = multipartFile.getOriginalFilename();//获取文件名
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());//获取文件后缀
        String saveName = DateUtils.getDateNowStr() + "." + suffix;//将文件名用时间重命名
        String path = Constant.filePath + "/" + SessionHelp.getStoreId(session);//文件上传路径
        staffWorks.setPictureFile(saveName);
        staffWorks.setPictureFilePath(path);
        InputStream inputStream = null;
        boolean b = false;
        try {
            inputStream = multipartFile.getInputStream();
            b = new FtpUtils().uploadFile(path,saveName,inputStream);
        } catch (IOException e) {
            log.error("SsmStaffWorksController.add:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FILE_UPLOAD_FAILED);
            return genericResponse;
        }

//        File file = new File(path);
//        if(!file.exists()){//目录不存在就创建
//            file.mkdirs();
//        }
//        try {
//            multipartFile.transferTo(new File(path +"/" + saveName));//保存文件
//        } catch (IOException e) {
//            log.error("SsmStaffWorksController.add:",e);
//            genericResponse.setCodeAndMsg(ResponseCode.FILE_UPLOAD_FAILED);
//            return genericResponse;
//        }
        if (b) {
            int i = 0;
            try {
                i = ssmStaffWorksService.addStaffWorks(staffWorks, SessionHelp.getLoginAccount(session));
            } catch (Exception e) {
                log.error("SsmStaffWorksController.add:", e);
                genericResponse.setCodeAndMsg(ResponseCode.FAILED);
                return genericResponse;
            }
            if (i > 0) {
                genericResponse.setCodeAndMsg(ResponseCode.OK);
            }
        }
        return genericResponse;
    }

}
