package cn.stylefeng.guns.zjxl.cnotroller;

import cn.stylefeng.guns.config.FastDFSConfig;
import cn.stylefeng.guns.zjxl.model.UploadModel;
import cn.stylefeng.guns.zjxl.model.ZjxlBanner;
import cn.stylefeng.guns.zjxl.model.ret.BannerCompanyCaseRet;
import cn.stylefeng.guns.zjxl.model.ret.BaseResultModel;
import cn.stylefeng.guns.zjxl.model.ret.ResultOneModel;
import cn.stylefeng.guns.zjxl.service.ZjxlBannerService;
import cn.stylefeng.guns.zjxlUtil.BaseController;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/***
 * @ClassName: ZjxlBannerCnotroller
 * @Description:
 * @Auther: shi kejing
 * @Date: 2020/10/30 17:37
 */
@RestController
@Api(description = "公司官网主页")
@RequestMapping(value = "/zjxl/banner")
public class ZjxlBannerCnotroller extends BaseController {

    @Autowired
    private ZjxlBannerService bannerService;

    @Autowired
    FastDFSConfig fastDFSConfig;
    @Value("${fastDFS.fastdfs_file_url}")
    private String fastdfs_file_url;

    @RequestMapping(value = "/findBannerById", method = RequestMethod.GET)
    @ApiOperation(value = "查看banner列表")
    public String findBannerById(@ApiParam(name = "id", value = "bannerId", required = false) @RequestParam(value = "id", required = false)String id,
                                 @ApiParam(name = "name", value = "bannerName", required = false) @RequestParam(value = "name", required = false)String name,
                                 @ApiParam(name = "page", value = "第几页", defaultValue = "0") @RequestParam(value = "page", required = false) Integer page,
                                 @ApiParam(name = "pagesize", value = "分页大小", defaultValue = "10") @RequestParam(value = "pagesize", required = false) Integer pagesize){
        try {
            List<ZjxlBanner> banner = bannerService.findById(id,name,page,pagesize);
            return write(200,"查询成功","data",banner);
        }catch (Exception e){
            e.printStackTrace();
            return error(-1,"查询失败");
        }
    }

    @RequestMapping(value = "/addBanner", method = RequestMethod.POST)
    @ApiOperation(value = "添加banner")
    public String addBanner(@ApiParam(name = "bannerName", value = "bannerName", required = false) @RequestParam(value = "bannerName", required = false)String bannerName,
                            @ApiParam(name = "bannerImage", value = "图片", required = false) @RequestParam(value = "bannerImage", required = false)String bannerImage,
                            @ApiParam(name = "bannerType", value = "跳转方式", required = false) @RequestParam(value = "bannerType", required = false)Integer bannerType,
                            @ApiParam(name = "bannerTypeUrl", value = "跳转链接", required = false) @RequestParam(value = "bannerTypeUrl", required = false)String bannerTypeUrl,
                            @ApiParam(name = "bannerSort", value = "排序", required = false) @RequestParam(value = "bannerSort", required = false)Integer bannerSort,
                            @ApiParam(name = "bannerIsLine", value = "是否上线", required = false) @RequestParam(value = "bannerIsLine", required = false)Integer bannerIsLine){
        ZjxlBanner banner = new ZjxlBanner();
        banner.setBannerImage(bannerImage);//String
        banner.setBannerIsLine(bannerIsLine);//integer
        banner.setBannerName(bannerName);//string
        banner.setBannerSort(bannerSort);
        banner.setBannerType(bannerType);
        banner.setBannerTypeUrl(bannerTypeUrl);
        try {
            return write(200,"添加成功","data",bannerService.addBanner(banner));
        }catch (Exception e){
            return write(-1,"添加失败");
        }
    }

    @RequestMapping(value = "/deleteBannerById", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除banner")
    public String deleteBannerById(String id){
        try {
            return write(200,"删除成功","data",bannerService.deleteBannerById(id));
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"删除失败");
        }
    }

    @RequestMapping(value = "/updateIsLine", method = RequestMethod.POST)
    @ApiOperation(value = "修改是否上线")
    public String updateIsLine(String id,Integer isLine){
        try {
            return write(200,"修改成功","data",bannerService.updateIsLine(id,isLine));
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"修改失败");
        }
    }

    /**
     * 聊天附件上传
     *
     * @return
     * @throws IOException
     * @throws IllegalStateException
     */
    @RequestMapping(value = "/file", method = RequestMethod.POST)
    @ApiOperation(value = "单文件上传")
    public ResultOneModel<UploadModel> uploadFile(
            @ApiParam(value = "文件", required = true) @RequestParam(value = "file", required = true) MultipartFile file
    ) {
        try {
            UploadModel uploadModel = fileUpload(file);
            return new ResultOneModel<UploadModel>(uploadModel);
        } catch (Exception e) {
            return new ResultOneModel(BaseResultModel.statusEm.file_upload_error.getCode(), BaseResultModel.statusEm.file_upload_error.getMessage() + "," + e.getMessage(), new JSONObject());
        }
    }

    @RequestMapping(value = "/finfBannerCompanyCaseById", method = RequestMethod.GET)
    @ApiOperation(value = "两表查询，把结果放一个实体类返回(测试)")
    public List<BannerCompanyCaseRet> finfBannerCompanyCaseById(String id){
       return bannerService.finfBannerCompanyCaseById(id);
    }

    /**
     * 文件上传到fastDfs
     *
     * @param mf
     * @return
     */
    private UploadModel fileUpload(MultipartFile mf) throws Exception {
        UploadModel uploadModel = new UploadModel();
        // 得到文件的完整名称  xxx.txt
        String fullName = mf.getOriginalFilename();
        //得到文件类型
        String fileType = fullName.substring(fullName.lastIndexOf(".") + 1).toLowerCase();
        String fileName = fullName.substring(0, fullName.lastIndexOf("."));
        //上传到fastdfs
        ObjectNode objectNode = fastDFSConfig.fastDFSUtil().upload(mf.getInputStream(), fileType, "");
        //解析返回的objectNode
        uploadModel.setFileName(fileName);
        uploadModel.setFileType(fileType);
        uploadModel.setFullUri(objectNode.get("fid").toString().replaceAll("\"", ""));
        uploadModel.setFullUrl(fastdfs_file_url + objectNode.get("fid").toString().replaceAll("\"", ""));
        return uploadModel;
    }
}
