package com.bweb.controller.file;

import com.bweb.controller.cms.CmsContentController;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.SessionObject;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.FileRecords;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.service.file.FileRecordsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
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 javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value="/file")
@Api(value="文件操作")
public class FileController {

    Logger log = LoggerFactory.getLogger(CmsContentController.class);

    @Resource
    private FileRecordsService fileRecordsService;

    @Resource
    private UserCenter userCenter;


    @RequestMapping(value="/getFileRecordsList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询列表")
    public ResponseData getFileRecordsList( HttpServletRequest request) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            SecUserSessionObject userModel = (SecUserSessionObject) sessionObject.getObject();

            Map<String, Object> map = new HashMap<>();
            map.put("opId", userModel.getAccount().getId());
            return ResponseMsgUtil.success(fileRecordsService.getList(map));

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/findFileRecords",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询详情")
    public ResponseData findFileRecords(
            @RequestParam(value = "id" , required = true) Long id) {
        try {

            return ResponseMsgUtil.success(fileRecordsService.queryDetail(id));

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getDownLoad",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "下载")
    public ResponseData getDownLoad(
            @RequestParam(value = "id" , required = true) Long id) {
        try {

            FileRecords fileRecords = fileRecordsService.queryDetail(id);
            if (fileRecords.getStatus() != 1) {
                throw ErrorHelp.genException(SysCode.System , ErrorCode.COMMON_ERROR , "当前状态错误！");
            }

            fileRecords.setStatus(1);
            fileRecords.setUpdateTime(new Date());
            fileRecords.setDownloadStatus(1);
            fileRecords.setDownloadTime(new Date());
            fileRecordsService.update(fileRecords);

            return ResponseMsgUtil.success(fileRecords.getFileUrl());

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
