package lingtao.net.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lingtao.net.bean.Msg;
import lingtao.net.bean.ProductImg;
import lingtao.net.bean.SysDictProduct;
import lingtao.net.service.ProductImgService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.management.RuntimeErrorException;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ProductImgController {

    private String localPrefix = "abc\\product";

    private String localPath = "C:\\lingtao\\upload";

    private String localDomain = "http://47.114.150.226:80/erp";

    @Autowired
    private ProductImgService productImgService;

    /**
     * 根据条件查询
     */
    @RequestMapping("/getImgList")
    private Msg imgList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                        @RequestParam(value = "limit", defaultValue = "10") Integer limit, ProductImg productImg) {
        PageHelper.startPage(page, limit);
        List<ProductImg> list = productImgService.getProKindList(productImg);
        PageInfo<ProductImg> pageInfo = new PageInfo<ProductImg>(list);
        return Msg.success().add("list", pageInfo);
    }

    /**
     * 获取所有产品
     *
     * @return
     */
    @RequestMapping("/productList")
    public Msg productList() {
        Map<String, String> map = new HashMap<String, String>();
        List<SysDictProduct> proList = productImgService.findAllPro();
        for (SysDictProduct entity : proList) {
            map.put(entity.getProTypeValue(), entity.getProTypeLabel());
        }
        return Msg.success().add("proMap", map);
    }

    /**
     * 根据产品种类获取材质
     *
     * @param proTypeValue
     * @return
     */
    /*
     * @RequestMapping("/getKindsByPro") public Msg
     * kindList(@RequestParam("proTypeValue") String proTypeValue) { Map<String,
     * String> map = new HashMap<String, String>(); List<SysDictProduct> kindList =
     * productImgService.getKindsByPro(proTypeValue); if (kindList.size() > 0) { for
     * (SysDictProduct entity : kindList) { map.put(entity.getKindValue(),
     * entity.getKindLabel()); } } return Msg.success().add("proMap", map); }
     */

    /**
     * 根据材质获取规格
     *
     * @param dto
     * @return
     */
    @RequestMapping(value = "/getKind2sByKind")
    public Map<String, String> kind2List(@RequestParam("proTypeValue") String proTypeValue,
                                         @RequestParam("kindValue") String kindValue) {
        Map<String, String> map = new HashMap<String, String>();
        List<SysDictProduct> kind2List = productImgService.getKind2sByKind(proTypeValue, kindValue);
        if (kind2List.size() > 0) {
            for (SysDictProduct entity : kind2List) {
                map.put(entity.getKind2Value(), entity.getKind2Label());
            }
        }
        return map;
    }

    // 图片上传及新增
    @RequestMapping("/productUpload")
    public Msg upload(@RequestParam("file") MultipartFile file,
                      @RequestParam(value = "proTypeValue", required = false) String proTypeValue,
                      @RequestParam(value = "kindValue", required = false) String kindValue,
                      @RequestParam(value = "kind2Value", required = false) String kind2Value,
                      @RequestParam(value = "remark", required = false) String remark) throws Exception {
        if (file.isEmpty()) {
            return Msg.fail("文件不能为空");
        }
        // 获取文件名后缀
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        // 获取path
        String path = getPath(extension, FilenameUtils.getBaseName(file.getOriginalFilename()));
        // 保存文件信息
        File newFile = new File(localPath + File.separator + path);
        try {
            FileUtils.copyInputStreamToFile(new ByteArrayInputStream(file.getBytes()), newFile);
        } catch (IOException e) {
            throw new RuntimeErrorException(null, "");
        }

        System.out.println("kind2Value=" + "null".equals(kind2Value));
        // 文件信息
        ProductImg productImg = new ProductImg();
        // productImg.setImgUrl(localPath + "/" + path);
        productImg.setImgUrl(localDomain + "/" + path);

        // 保存图片信息
        productImg.setProTypeValue(proTypeValue);
        if (StringUtils.isNotEmpty(kindValue)) {
            productImg.setKindValue(kindValue);
        }
        if (StringUtils.isNotEmpty(kind2Value) && !"null".equals(kind2Value)) {
            productImg.setKind2Value(kind2Value);
        }
        if (StringUtils.isNotEmpty(remark)) {
            productImg.setRemark(remark);
        }
        // 根据value查询label
        SysDictProduct label = productImgService.getLabel(productImg);
        productImg.setProTypeLabel(label.getProTypeLabel());
        productImg.setKindLabel(label.getKindLabel());
        productImg.setKind2Label(label.getKind2Label());

        // productImg.setImgUrl(productImg.getImgUrl());
        productImgService.addImgUrl(productImg);

        return Msg.success().add("imgList", productImg);
    }

    /**
     * 修改备注说明
     *
     * @param id
     * @param remark
     * @return
     */
    @RequestMapping("/updateImgUploadRemark")
    public Msg updateImgUploadRemark(@RequestParam("id") Long id, @RequestParam("remark") String remark) {
        ProductImg img = new ProductImg();
        img.setId(id);
        img.setRemark(remark);
        productImgService.updateImgUploadRemark(img);
        return Msg.success();
    }

    /**
     * @param prefixSelf 根据上传的接口存入自己的文件夹
     * @param suffix     文件的后缀
     * @param fileName   文件名
     * @return
     */
    public String getPath(String suffix, String fileName) {

        // 生成uuid
        // String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String path = null;

        // 文件路径
        path = DateUtils.formatDate(new Date(), "yyyyMMdd") + File.separator + fileName;

        path = localPrefix + File.separator + File.separator + path;

        return path + "." + suffix;
    }

    /**
     * 根据条件获取产品图片
     *
     * @param dto
     * @return
     */
    @RequestMapping("/getImgs")
    public List<ProductImg> imgList(@RequestParam("proTypeValue") String proTypeValue,
                                    @RequestParam(value = "kindValue", required = false) String kindValue,
                                    @RequestParam(value = "craftValue", required = false) String craftValue,
                                    @RequestParam(value = "kind2Value", required = false) String kind2Value) {
        List<ProductImg> imgList = productImgService.getImgsByProKind(proTypeValue, kindValue, kind2Value, craftValue);
        return imgList;
    }

    /**
     * 获取视频地址
     *
     * @param dto
     * @return
     */
    @RequestMapping("/getVideos")
    public List<String> videos() {
        List<String> videoUrls = productImgService.getVideos();
        return videoUrls;
    }
}
