package com.blueland.render.rest;


/**
 * @author 代码自动生成工具
 * @version 1.0
 * @since
 */

import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blueland.annotation.Log;
import com.blueland.bo.*;
import com.blueland.model.TPfAttachment;
import com.blueland.render.config.CustomConfig;
import com.blueland.result.R;
import com.blueland.service.IAttachmentService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import static com.blueland.constant.CommonConstant.*;
import static com.blueland.constant.ResponseConstant.*;


@Api(value = "attachment")
@RestController
@RequestMapping(value = "/api/attachment")
public class AttachmentRest {
    @Autowired
    private IAttachmentService attachmentService;
    @Autowired
    private CustomConfig customConfig;


    /**
     * 上传方法
     * @param request
     * @param response
     */
    @RequestMapping("/uploadfiles")
    @ResponseBody
    public Map<String, Object> uploadFiles(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        String path = customConfig.getUpload();//上传路径
        String appId = request.getParameter("appId");
        try {
            result = attachmentService.uploadFile(request, path, null, null);
            if ("true".equals(result.get("result"))) {
                TPfAttachment attachment = new TPfAttachment();
                String fileName = result.get("fileName").toString();
                attachment.setAttName(fileName);
                attachment.setAttUrl(result.get("uploadUrl").toString());
                attachment.setAttType(result.get("contentType").toString());
                attachment.setCreateTime(new Date());
                attachment.setCreateUserId("czadmin");
                attachment.setUpdateUserId("czadmin");
                attachment.setAppId(appId);
                attachment.setAttShow(request.getParameter("attShow"));
                attachmentService.save(attachment);
                String uploadDirUrl = attachment.getAttUrl();
                attachmentService.asynFileForSyy(attachment.getAttId(), uploadDirUrl, false);
                result.put("fileId", attachment.getAttId());//回写数据库附件表文件id
                result.put("fileName", attachment.getAttName());//回写数据库附件表文件id
                attachment = attachmentService.getById(attachment.getAttId());
                result.put("attachment", attachment);
                result.put("resultCode", RESULT_CODE_SUCCESS_CODE);

            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return result;
    }

    /**
     * 搜索对象
     **/
    @RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @CrossOrigin
    public Page<AttachmentEntity> list(AttachmentEntity attachmentEntity, Integer curPage, Integer curPageSize) {
        if (curPageSize == null) {
            curPageSize = 100;
        }
        if (curPage == null) {
            curPage = 1;
        }
        Page<AttachmentEntity> page = this.attachmentService.findPageList(attachmentEntity, curPage, curPageSize);
        return page;
    }


    /**
     * 查看对象
     **/
    @RequestMapping(value = "/{attId}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @CrossOrigin
    public R<TPfAttachment> view(@PathVariable String attId) {
        TPfAttachment attachmentEntity = attachmentService.getById(attId);
        return R.ok(attachmentEntity);
    }


    /**
     * 查看对象（支持多条件查询）
     **/
    @RequestMapping(value = "get", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @CrossOrigin
    public R<TPfAttachment> get(AttachmentEntity model) {
        TPfAttachment attachmentEntity = attachmentService.get(model);
        return R.ok(attachmentEntity);
    }


    /**
     * 新增对象
     **/
    @Log(busiType = "新增附件", opType = SAVE, type = "render")
    @RequestMapping(value = "", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @CrossOrigin
    public R<AttachmentEntity> save(@RequestBody AttachmentEntity model) {
        TPfAttachment attachment = BeanUtil.copyProperties(model, TPfAttachment.class);
        attachmentService.save(attachment);
        return R.ok(model);
    }


    /**
     * 更新对象
     * @return
     **/
    @Log(busiType = "更新附件", opType = UPDATE, type = "render")
    @RequestMapping(value = "", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @CrossOrigin
    public R<AttachmentEntity> update(@RequestBody AttachmentEntity model) {
        TPfAttachment attachment = BeanUtil.copyProperties(model, TPfAttachment.class);
        attachmentService.updateById(attachment);
        return R.ok(model);
    }

    /**
     * 删除对象
     * @return
     **/
    @Log(busiType = "删除附件", opType = DELETE, type = "render")
    @RequestMapping(value = "/{attId}", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @CrossOrigin
    public R<Boolean> delete(@PathVariable String attId) {
        attachmentService.removeById(attId);
        return R.ok(true);
    }

    /**
     *  批量删除对象
     * @return
     **/
    @Log(busiType = "批量删除附件", opType = DELETE, type = "render")
    @RequestMapping(value = "deleteBatch", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @CrossOrigin
    public R<Boolean> deleteBatch(String[] ids) {
        if (ObjectUtil.isEmpty(ids)) {
            throw new RuntimeException("id不能为空");
        }
        for (int i = 0; i < ids.length; i++) {
            attachmentService.removeById(ids[i]);
        }
        return R.ok(true);
    }

}



