package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.PageResponse;
import com.ssm.manage.entity.*;
import com.ssm.manage.service.SsmBaseStoreService;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.core.utils.FtpUtils;
import com.ssm.manage.query.SmsRechargeListRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SmsRechargeListService;
import com.ssm.manage.utils.Constant;
import io.swagger.annotations.*;
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.*;

@Api(value="短信充值controller",tags={"短信充值"})
@RestController
@RequestMapping("/demay/ssm/smsRechargeList")
@Slf4j
public class SmsRechargeListController {
    @Autowired
    private SmsRechargeListService smsRechargeListService;
    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;


    @ApiOperation(value = "新增", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/add")
    public GenericResponse add(List<MultipartFile> fileList, SmsRechargeListRequest smsRechargeList, HttpSession session) {
        GenericResponse<SmsRechargeList> genericResponse = new GenericResponse<>();
        if (null == fileList || fileList.size() == 0) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "文件");
            return genericResponse;
        }
        //查询门店的短信账号
        BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(smsRechargeList.getStoreId());
        smsRechargeList.setStoreNum(baseStore.getStoreNum());
        if(baseStore.getSmsAccount()==null||baseStore.getSmsAccount().equals("")){
            genericResponse.setCodeAndMsg(ResponseCode.MSG, "请联系管理员分配短信账号");
            return genericResponse;
        }
        List<UploadFiles> uploadFilesInfo = new ArrayList<>();
        FtpUtils ftp = new FtpUtils();
        for (MultipartFile multipartFile : fileList) {
            UploadFiles uploadFiles = new UploadFiles();
            String fileName = multipartFile.getOriginalFilename();//获取文件名
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());//获取文件后缀
            String saveName = UUID.randomUUID() + "." + suffix;//将文件名用时间重命名
            String path = Constant.filePath + "/" + SessionHelp.getStoreId(session);//文件上传路径
            uploadFiles.setFileName(saveName);
            uploadFiles.setFilePath(path);
            uploadFilesInfo.add(uploadFiles);
            InputStream inputStream = null;
            try {
                inputStream = multipartFile.getInputStream();
                ftp.uploadFile(path, saveName, inputStream);
            }catch (IOException e) {
                log.error("SsmRefundOrderController.add:", e);
                genericResponse.setCodeAndMsg(ResponseCode.FILE_UPLOAD_FAILED);
                return genericResponse;
            }
        }
        smsRechargeList.setUploadFiles(uploadFilesInfo);
        String loginAccount = SessionHelp.getLoginAccount(session);
        int i;
        try {
            i = smsRechargeListService.saveRechargeList(smsRechargeList,loginAccount);

        } catch (SmsException e) {
            log.error("SsmProjectController.add", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }


    @ApiOperation(value = "查询", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/page")
    public PageResponse<SmsRechargeList> page(@RequestBody SmsRechargeListRequest smsRechargeListRequest, HttpSession session) {
        PageResponse<SmsRechargeList> genericResponse = new PageResponse<>();
        if(smsRechargeListRequest.getStoreId()==null){
            smsRechargeListRequest.setStoreId(SessionHelp.getStoreId(session));
            if(smsRechargeListRequest.getStoreId()==null){
                genericResponse.setCodeAndMsg(ResponseCode.MSG,"请选择门店");
                return genericResponse;
            }
        }
        PageInfo<SmsRechargeList> select = smsRechargeListService.selectByList(smsRechargeListRequest);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setPageInfo(select);

        return genericResponse;
    }

    @ApiOperation(value = "平台查询", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/pageByPingtai")
    public PageResponse pageByPingtai(@RequestBody SmsRechargeListRequest smsRechargeListRequest, HttpSession session) {
        PageResponse<SmsRechargeList> genericResponse = new PageResponse<>();
        PageInfo<SmsRechargeList> select = smsRechargeListService.selectByList(smsRechargeListRequest);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setPageInfo(select);
        return genericResponse;
    }

    @ApiOperation(value = "修改", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/update")
    public GenericResponse update(List<MultipartFile> fileList, @RequestBody SmsRechargeListRequest smsRechargeListRequest, HttpSession session) {
        GenericResponse<SmsRechargeList> genericResponse = new GenericResponse<>();
        List<UploadFiles> uploadFilesInfo = new ArrayList<>();
        FtpUtils ftp = new FtpUtils();
        for (MultipartFile multipartFile : fileList) {
            UploadFiles uploadFiles = new UploadFiles();
            String fileName = multipartFile.getOriginalFilename();//获取文件名
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());//获取文件后缀
            String saveName = UUID.randomUUID() + "." + suffix;//将文件名用时间重命名
            String path = Constant.filePath + "/" + SessionHelp.getStoreId(session);//文件上传路径
            uploadFiles.setFileName(saveName);
            uploadFiles.setFilePath(path);
            uploadFilesInfo.add(uploadFiles);
            InputStream inputStream = null;
            try {
                inputStream = multipartFile.getInputStream();
                ftp.uploadFile(path, saveName, inputStream);
            }catch (IOException e) {
                log.error("SsmRefundOrderController.add:", e);
                genericResponse.setCodeAndMsg(ResponseCode.FILE_UPLOAD_FAILED);
                return genericResponse;
            }
        }
        smsRechargeListRequest.setUploadFiles(uploadFilesInfo);
        String loginAccount = SessionHelp.getLoginAccount(session);
        try {
             smsRechargeListService.updateByRechargeList(smsRechargeListRequest,loginAccount);

        } catch (Exception e) {
            log.error("SsmProjectController.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);

        return genericResponse;
    }

    @ApiOperation(value = "删除", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/delete")
    public GenericResponse delete(@RequestBody SmsRechargeList smsRechargeList, HttpSession session) {
        GenericResponse<SmsFix> genericResponse = new GenericResponse<>();
        //获取添加人的名称
        int i = 0;
        try {
            i = smsRechargeListService.deleteById(smsRechargeList.getId());

        } catch (Exception e) {
            log.error("SsmProjectController.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if(i ==999){
            genericResponse.setCodeAndMsg(ResponseCode.FAILEDS);
            return genericResponse;
        }else if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }


    @ApiOperation(value = "送审", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/sendAudit")
    @ApiImplicitParams(@ApiImplicitParam(name = "id" ,value = "短信充值单id",required = true))
    public GenericResponse sendAudit(@RequestBody SmsRechargeListRequest smsRechargeListRequest, HttpSession session) {
        GenericResponse<SmsRechargeList> genericResponse = new GenericResponse<>();
        int i =0;
        try {
          i=  smsRechargeListService.sendAudit(smsRechargeListRequest,session);

        }catch (SmsException e) {
            log.error("SsmProjectController.add", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmProjectController.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if(i>0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }else {
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"当前状态不允许送审");

        }

        return genericResponse;
    }

    @ApiOperation(value = "审核", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/audit")
    @ApiImplicitParams(@ApiImplicitParam(name = "id" ,value = "短信充值单id",required = true))
    public GenericResponse audit(@RequestBody SmsRechargeListRequest smsRechargeListRequest, HttpSession session) {
        GenericResponse<SmsRechargeList> genericResponse = new GenericResponse<>();
        try {
            smsRechargeListRequest.setState(3);
            smsRechargeListService.audit(smsRechargeListRequest,session);
        }catch (SmsException e) {
            log.error("SsmProjectController.add", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmProjectController.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);

        return genericResponse;
    }

    @ApiOperation(value = "反审核", httpMethod = "POST", response = GenericResponse.class)
    @ApiImplicitParams(@ApiImplicitParam(name = "id" ,value = "短信充值单id",required = true))
    @RequestMapping("/unAudit")
    public GenericResponse unAudit(@RequestBody SmsRechargeListRequest smsRechargeListRequest, HttpSession session) {
        GenericResponse<SmsRechargeList> genericResponse = new GenericResponse<>();
        try {
            smsRechargeListRequest.setState(4);
            smsRechargeListService.unAudit(smsRechargeListRequest,session);

        }catch (SmsException e) {
            log.error("SsmProjectController.add", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmProjectController.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);

        return genericResponse;
    }

    @ApiOperation(value = "回收作废", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/cancel")
    @ApiImplicitParams(@ApiImplicitParam(name = "id" ,value = "短信充值单id",required = true))
    public GenericResponse cancel(@RequestBody SmsRechargeListRequest smsRechargeListRequest, HttpSession session) {
        GenericResponse<SmsRechargeList> genericResponse = new GenericResponse<>();
        try {
            smsRechargeListRequest.setState(5);
            smsRechargeListService.cancel(smsRechargeListRequest,session);

        }catch (SmsException e) {
            log.error("SsmProjectController.add", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmProjectController.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);

        return genericResponse;
    }

}
