/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ifish.helper;

import com.ifish.bean.CommodityInfoBean;
import com.ifish.dto.JsonResult;
import com.ifish.dto.PageingDto;
import com.ifish.enums.ResultEnum;
import com.ifish.exception.IfishException;
import com.ifish.mapper.Tbl_Commodity_Info_Mapper;
import com.ifish.util.IfishFilePath;
import java.io.File;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import com.ifish.util.IfishUtil;

/**
 * 商品管理所有接口方法
 *
 * @author Administrator
 */
@Component
public class CommodityInfoHelper implements CommodityInfoHelperI {

    @Autowired
    private Tbl_Commodity_Info_Mapper tbl_Commodity_Info_Mapper;

    @Autowired
    private FastDFSClientI fastDFSClientI;

    @Override
    public JsonResult<?> createCommodityInfo(CommodityInfoBean commodityInfoBean, MultipartFile img, MultipartFile vedio) {
        try {
            //保存图片
            if (img != null) {
                if (!img.getOriginalFilename().equals("")) {
                    String filename = String.valueOf(System.currentTimeMillis()) + ".png";
                    File picFile = new File(IfishFilePath.path_commodity + "/" + commodityInfoBean.getUserId(), filename);
                    //不存在则创建
                    if (!picFile.exists()) {
                        picFile.mkdirs();
                    }
                    //保存
                    img.transferTo(picFile);
                    commodityInfoBean.setCommodityImg(filename);
                } else {
                    return new JsonResult(ResultEnum.warn222.getKey(), "");
                }
            } else {
                return new JsonResult(ResultEnum.warn223.getKey(), "");
            }
            //保存视频
            if (vedio != null) {
                String url = fastDFSClientI.uploadFileToFastDFS(vedio);
                if (StringUtils.isNotBlank(url)) {
                    commodityInfoBean.setCommodityVideo(url);
                } else {
                    return new JsonResult(ResultEnum.warn223.getKey(), "");
                }
            } else {
                return new JsonResult(ResultEnum.warn223.getKey(), "");
            }

            int i = tbl_Commodity_Info_Mapper.createCommodityInfo(commodityInfoBean);
            if (i > 0) {
                int id = commodityInfoBean.getCommodityId();
                commodityInfoBean = tbl_Commodity_Info_Mapper.getCommodityInfoByID(id);
                return new JsonResult(ResultEnum.success.getKey(), commodityInfoBean);
            } else {
                return new JsonResult(ResultEnum.fail101.getKey(), "");
            }
        } catch (Exception e) {
            return new JsonResult(ResultEnum.error401.getKey(), "");
        }
    }

    @Override
    public JsonResult<?> updateCommodityInfo(CommodityInfoBean commodityInfoBean) {
        if (commodityInfoBean.getCommodityId() <= 0) {
            //ID不能为空
            return new JsonResult(ResultEnum.error401.getKey(), "");
        }
        try {
            String str = commodityInfoBean.getCommodityName();
            if (StringUtils.isNotBlank(str)) {
                commodityInfoBean.setCommodityName(str.substring(str.indexOf(',') + 1, str.length()));
            }
            int i = tbl_Commodity_Info_Mapper.updateCommodityInfo(commodityInfoBean);
            if (i > 0) {
                int id = commodityInfoBean.getCommodityId();
                commodityInfoBean = tbl_Commodity_Info_Mapper.getCommodityInfoByID(id);
                return new JsonResult(ResultEnum.success.getKey(), commodityInfoBean);
            } else {
                //修改失败
                return new JsonResult(ResultEnum.fail101.getKey(), "");
            }
        } catch (Exception e) {
            //修改出错
            return new JsonResult(ResultEnum.error401.getKey(), "");
        }
    }

    @Override
    public JsonResult<?> getCommodityInfo(Integer commodityId) {
        if (commodityId <= 0) {
            return new JsonResult(ResultEnum.error401.getKey(), "");
        }
        try {
            CommodityInfoBean commodityInfoBean = tbl_Commodity_Info_Mapper.getCommodityInfoByID(commodityId);
            if (commodityInfoBean != null && commodityInfoBean.getCommodityId() >= 0) {
                return new JsonResult(ResultEnum.success.getKey(), commodityInfoBean);
            } else {
                return new JsonResult(ResultEnum.fail101.getKey(), "");
            }
        } catch (Exception e) {
            return new JsonResult(ResultEnum.error401.getKey(), "");
        }
    }

    @Override
    public PageingDto<?> getCommodityInfoByPage(Integer pageSize, Integer firstResult, Integer shopId, Integer commodityState, String orderBy) {
        try {
            if (StringUtils.isBlank(orderBy)) {
                orderBy = "create_time";
            }
            PageingDto<CommodityInfoBean> page = new PageingDto<>();
            List<CommodityInfoBean> list = tbl_Commodity_Info_Mapper.getCommodityInfoByPage(shopId, firstResult, pageSize, commodityState, orderBy);
            int count = tbl_Commodity_Info_Mapper.getCommodityInfoCountByPage(shopId, commodityState);
            page.setTotal(count);
            page.setFirstResult(firstResult);
            page.setData(list);
            page.setResult("100");
            return page;
        } catch (Exception e) {
            throw new IfishException(ResultEnum.fail101);
        }
    }

    @Override
    public JsonResult<?> deleteCommodityInfo(Integer commodityId) {
        if (commodityId <= 0) {
            return new JsonResult(ResultEnum.error401.getKey(), "");
        }
        try {
            int i = tbl_Commodity_Info_Mapper.deleteCommodityInfoById(commodityId);
            if (i > 0) {
                return new JsonResult(ResultEnum.success.getKey(), "");
            } else {
                return new JsonResult(ResultEnum.fail101.getKey(), "");
            }
        } catch (Exception e) {
            return new JsonResult(ResultEnum.error401.getKey(), "");
        }
    }
}
