package com.org.hktshop.service.brand;

import com.aliyun.oss.OSSClient;
import com.org.hktshop.Interface.brand.IHktBrandCategoryService;
import com.org.hktshop.Interface.brand.IHktBrandDescService;
import com.org.hktshop.Interface.brand.IHktBrandService;
import com.org.hktshop.base.BaseService;
import com.org.hktshop.base.MongoHelper;
import com.org.hktshop.entity.Page;
import com.org.hktshop.entity.admin.HktAdmin;
import com.org.hktshop.entity.brand.HktBrand;
import com.org.hktshop.entity.brand.HktBrandCategory;
import com.org.hktshop.entity.brand.HktBrandDesc;
import com.org.hktshop.entity.vo.HktBrandInfo;
import com.org.hktshop.properties.AliyunOSSProperties;
import com.org.hktshop.service.aliyun.AliyunOssService;
import com.org.hktshop.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@Service
public class HktBrandServiceImpl extends BaseService<HktBrand> implements IHktBrandService {

    Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    IHktBrandDescService descService;
    @Autowired
    IHktBrandCategoryService brandCategoryService;
    @Autowired
    MongoHelper<HktBrandInfo> mongoHelper;


    @Autowired
    private AliyunOSSProperties aliyunOSSProperties;

    @Autowired
    private AliyunOssService aliyunOssService;


    @Override
    public List<HktBrandInfo> queryLists(boolean isDelete) {

        List<HktBrandInfo> list = new ArrayList<>();
        if (isDelete) mongoHelper.delete(new Query(), HktBrandInfo.class);
        else list = mongoHelper.find(new HktBrandInfo(), HktBrandInfo.class);

        List<HktBrandCategory> categories = brandCategoryService.queryLists(false);
        Map<String, HktBrandCategory> hktBrandCategoryMap = new HashMap<>();
        for (HktBrandCategory category : categories) hktBrandCategoryMap.put(category.getId(), category);

//        list.clear();
        if (list.isEmpty()) {
            List<HktBrand> brands = queryList(new HktBrand());
            List<HktBrandDesc> descs = descService.queryList(new HktBrandDesc());

//            List<String> sqlList = new ArrayList<>();
//            for (HktBrand b : brands) {
//                if (!StringUtils.isNull(b.getLogo())) {
//                    OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
//                    try {
//                        String url = aliyunOssService.loadFileFromUrl(ossClient, b.getLogo(), "BrandInfo/logo");
//                        logger.info(b.getLogo() + "->>>" + url);
//                        b.setLogo(url);
//                        sqlList.add(ExecSqlLib.GetUpdateSql(b));
//                    } catch (UnsupportedEncodingException e) {
//                        e.printStackTrace();
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//            if (sqlList.size() > 0) {
//                this.baseJdbcDao.executeTransaction(sqlList);
//            }


//            List<String> sqlList = new ArrayList<>();
//            for (HktBrandDesc desc : descs) {
//                if (!StringUtils.isNull(desc.getLogoMax())) {
//                    OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
//                    try {
//
//
//
//                        String url = aliyunOssService.loadFileFromUrl(ossClient, desc.getLogoMax(), "BrandInfo/logoMax");
//
//                        logger.info(desc.getLogoMax()+ "->>>" + url);
//                        desc.setLogoMax(url);
//
//
//                    } catch (UnsupportedEncodingException e) {
//                        e.printStackTrace();
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//                if (!StringUtils.isNull(desc.getVideoUrl())) {
//                    OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
//                    try {
//                        String url = aliyunOssService.loadFileFromUrl(ossClient, desc.getVideoUrl(), "BrandInfo/logoMax");
//                        logger.info(desc.getVideoUrl()+ "->>>" + url);
//                        desc.setVideoUrl(url);
//                    } catch (UnsupportedEncodingException e) {
//                        e.printStackTrace();
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//
//                sqlList.add(ExecSqlLib.GetUpdateSql(desc));
//            }
//
//            if (sqlList.size() > 0) {
//                this.baseJdbcDao.executeTransaction(sqlList);
//            }


            for (HktBrand brand : brands) {

                HktBrandInfo info = new HktBrandInfo();
                info.setFno(brand.getFno());
                info.setId(brand.getId());
                info.setLogo(brand.getLogo());
                info.setCategoryId(brand.getCategoryId());
                info.setStatus(brand.getStatus());
                info.setSort(brand.getSort());
                info.setIsShow(brand.getIsShow());
                info.setIsHot(brand.getIsHot());
                info.setGoodsNum(brand.getGoodsNum());
                info.setOrderNum(brand.getOrderNum());
                info.setTotalAmount(brand.getTotalAmount());
                info.setCreateTime(brand.getCreateTime());
                info.setModifyTime(brand.getModifyTime());

                list.add(info);
            }

            for (HktBrandDesc desc : descs) {
                for (HktBrandInfo brand : list) {
                    if (brand.getFno().equals(desc.getBrandId())) {
                        if (desc.getLanguageId().equals(1)) brand.setBrandDesc_ch(desc);
                        else if (desc.getLanguageId().equals(2)) brand.setBrandDesc_en(desc);
                        break;
                    }
                }
            }

            mongoHelper.delete(new Query(), HktBrandInfo.class);
            for (HktBrandInfo brand : list) {

                mongoHelper.save(brand);
            }
        }

        for (HktBrandInfo info : list) {
            if (hktBrandCategoryMap.containsKey(info.getCategoryId())) {
                HktBrandCategory category = hktBrandCategoryMap.get(info.getCategoryId());
                info.setCategory_name(category.getName());
            }
        }

        return list;
    }

    @Override
    public Page<HktBrandInfo> queryPage(HktBrandInfo brand) {

        Query query;
        if(!StringUtils.isNull(brand.getKeyword())){
            Criteria criteria = new Criteria();
            Criteria a = Criteria.where("brandDesc_ch.name").is(brand.getKeyword());
            Criteria b = Criteria.where("brandDesc_en.name").regex(brand.getKeyword(),"i");
            Criteria c = Criteria.where("fno").is(brand.getKeyword());
            criteria.orOperator(a,b,c);
            query = new Query(criteria);
        }else {
            if (brand.getBrandDesc_ch() != null && !StringUtils.isNull(brand.getBrandDesc_ch().getBrandHead())) {
                Pattern pattern = Pattern.compile(brand.getBrandDesc_ch().getBrandHead());
                query = new Query(Criteria.where("brandDesc_ch.brandHead").regex(pattern));
            } else if (brand.getBrandDesc_en() != null && !StringUtils.isNull(brand.getBrandDesc_en().getBrandHead())) {
                Pattern pattern = Pattern.compile(brand.getBrandDesc_ch().getBrandHead());
                query = new Query(Criteria.where("brandDesc_en.brandHead").regex(pattern));
            } else if (!StringUtils.isNull(brand.getFno())) {
                query = new Query(Criteria.where("fno").regex(brand.getFno()));
            } else {
                query = mongoHelper.createQuery(brand, HktBrandInfo.class);
            }
        }
        query.with(new Sort(Sort.Direction.ASC, "sort"));
        Page<HktBrandInfo> page = mongoHelper.findPage(brand, query, HktBrandInfo.class);

        List<HktBrandCategory> categories = brandCategoryService.queryLists(false);
        Map<String, HktBrandCategory> hktBrandCategoryMap = new HashMap<>();
        for (HktBrandCategory category : categories) hktBrandCategoryMap.put(category.getId(), category);

        for (HktBrandInfo info : page.getRecords()) {
            if (hktBrandCategoryMap.containsKey(info.getCategoryId())) {
                HktBrandCategory category = hktBrandCategoryMap.get(info.getCategoryId());
                info.setCategory_name(category.getName());
            }
        }
        return page;
    }

    @Override
    public HktBrandInfo queryBrand(HktBrandInfo brand) {
        HktBrandInfo info = mongoHelper.findOne(brand, HktBrandInfo.class);

        List<HktBrandCategory> categories = brandCategoryService.queryLists(false);
        Map<String, HktBrandCategory> hktBrandCategoryMap = new HashMap<>();
        for (HktBrandCategory category : categories) hktBrandCategoryMap.put(category.getId(), category);

        if (hktBrandCategoryMap.containsKey(info.getCategoryId())) {
            HktBrandCategory category = hktBrandCategoryMap.get(info.getCategoryId());
            info.setCategory_name(category.getName());
        }


        Integer findType = 0;
        if (!StringUtils.isNull(brand.getLanguage())) {
            if (brand.getLanguage().equals("1")) {
                findType = 1;
            } else {
                findType = 2;
            }
        }

        if (findType == 0 || findType == 1) {
            if (!StringUtils.isNull(info.getBrandDesc_ch().getContent())) {
                OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
                info.getBrandDesc_ch().setContent(aliyunOssService.getTxtMsg(ossClient, info.getBrandDesc_ch().getContent()));
            }
            if (!StringUtils.isNull(info.getBrandDesc_ch().getInfo())) {
                OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
                info.getBrandDesc_ch().setInfo(aliyunOssService.getTxtMsg(ossClient, info.getBrandDesc_ch().getInfo()));
            }
        }
        if (findType == 0 || findType == 2) {
            if (!StringUtils.isNull(info.getBrandDesc_en().getContent())) {
                OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
                info.getBrandDesc_en().setContent(aliyunOssService.getTxtMsg(ossClient, info.getBrandDesc_en().getContent()));
            }
            if (!StringUtils.isNull(info.getBrandDesc_en().getInfo())) {
                OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
                info.getBrandDesc_en().setInfo(aliyunOssService.getTxtMsg(ossClient, info.getBrandDesc_en().getInfo()));
            }
        }

        return info;
    }

    private Ret CheckInput(HktBrandInfo info) {

        if (StringUtils.isNull(info.getBrandDesc_ch().getName()))
            return Ret.create().setFail().set("msg", String.format("商品%s名称不可为空", (info.getBrandDesc_ch().getLanguageId() == 1 ? "中文" : "英文")));

        if (StringUtils.isNull(info.getBrandDesc_en().getName()))
            return Ret.create().setFail().set("msg", String.format("商品%s名称不可为空", (info.getBrandDesc_en().getLanguageId() == 1 ? "中文" : "英文")));

        return Ret.create().setOk();
    }


    @Override
    public Ret updateBrand(HktAdmin opUser, HktBrandInfo brand) throws Exception {

        Ret ret = CheckInput(brand);
        if (ret.isFail()) return ret;

        HktBrandCategory category = new HktBrandCategory();
        category.setId(brand.getCategoryId());
        category = brandCategoryService.queryBrandCategory(category);
        if (category == null) return Ret.create().setFail().setMsg("品牌分类不存在");

        if (StringUtils.isNull(brand.getId())) {

            HktBrandInfo d = new HktBrandInfo();
            d.setFno(brand.getFno());
            HktBrandInfo find = mongoHelper.findOne(d, HktBrandInfo.class);
            if (find != null) return Ret.create().setFail().setMsg("该编码已被使用");

            //新增品牌
            //自增长id查询
            int count = this.baseJdbcDao.getAutoId(new HktBrand());
            brand.setId("" + count);
            brand.setCreateTime(DateUtil.getDateTime());
            brand.setGoodsNum(0);
            brand.setOrderNum(0);
            brand.setTotalAmount(BigDecimal.valueOf(0));
            if (StringUtils.isNull(brand.getIsHot())) brand.setIsHot(0);
            if (StringUtils.isNull(brand.getIsShow())) brand.setIsShow(1);


            HktBrand newBrand = brand.toFatherBean();
            List<String> sqls = new ArrayList<>();
            sqls.add(ExecSqlLib.GetInsertSql(newBrand));
            {
                //todo 品牌首字母怎么生成
                //中文
                String name = brand.getBrandDesc_ch().getName();
                String py = PinyinUtil.getPinYin(name);
                brand.getBrandDesc_ch().setBrandHead(py.substring(0, 1).toUpperCase() + brand.getBrandDesc_en().getName().substring(0, 1).toUpperCase());
                brand.getBrandDesc_ch().setBrandId(brand.getFno());
                brand.getBrandDesc_ch().setId("");
                brand.getBrandDesc_ch().setLanguageId(1);

                if (StringUtils.isNull(brand.getBrandDesc_ch().getContent())) brand.getBrandDesc_ch().setContent(" ");
                if (StringUtils.isNull(brand.getBrandDesc_ch().getInfo())) brand.getBrandDesc_ch().setInfo(" ");

                String url = null;
                try {
                    OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
                    url = aliyunOssService.uploadForStr(ossClient, brand.getBrandDesc_ch().getContent(), "BrandInfo/" + System.currentTimeMillis() + ".html");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                brand.getBrandDesc_ch().setContent(url);

                try {
                    OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
                    url = aliyunOssService.uploadForStr(ossClient, brand.getBrandDesc_ch().getInfo(), "BrandInfo/" + System.currentTimeMillis() + ".html");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                brand.getBrandDesc_ch().setInfo(url);

                sqls.add(ExecSqlLib.GetInsertSql(brand.getBrandDesc_ch()));

                //英文
                brand.getBrandDesc_en().setBrandHead(brand.getBrandDesc_en().getName().substring(0, 1).toUpperCase());
                brand.getBrandDesc_en().setBrandId(brand.getFno());
                brand.getBrandDesc_en().setId("");
                brand.getBrandDesc_en().setLanguageId(2);


                if (StringUtils.isNull(brand.getBrandDesc_en().getContent())) brand.getBrandDesc_en().setContent(" ");
                if (StringUtils.isNull(brand.getBrandDesc_en().getInfo())) brand.getBrandDesc_en().setInfo(" ");
                try {
                    OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
                    url = aliyunOssService.uploadForStr(ossClient, brand.getBrandDesc_en().getContent(), "BrandInfo/" + System.currentTimeMillis() + ".html");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                brand.getBrandDesc_en().setContent(url);

                try {
                    OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
                    url = aliyunOssService.uploadForStr(ossClient, brand.getBrandDesc_en().getInfo(), "BrandInfo/" + System.currentTimeMillis() + ".html");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                brand.getBrandDesc_en().setInfo(url);

                sqls.add(ExecSqlLib.GetInsertSql(brand.getBrandDesc_en()));
            }

            if (this.baseJdbcDao.execute(sqls)) {
                //成功
                HktBrandDesc desc = new HktBrandDesc();
                desc.setBrandId(brand.getFno());
                List<HktBrandDesc> descList = descService.queryList(desc);
                //品牌分类统计
                Integer num = category.getBrandNum();
                if (num == null) num = 1;
                else num += 1;
                category.setBrandNum(num);
                brandCategoryService.updateBrandCetagory(opUser, category);

                for (HktBrandDesc desc1 : descList) {
                    if (desc1.getLanguageId().equals(1)) brand.setBrandDesc_ch(desc1);
                    else if (desc1.getLanguageId().equals(2)) brand.setBrandDesc_en(desc1);
                }
                brand.setCategory_name(category.getName());
                mongoHelper.save(brand);
                return Ret.create().setOk().set("msg", "新增品牌成功");
            } else {
                //失败
                return Ret.create().setFail().set("msg", "新增品牌失败");
            }
        } else {
            //修改品牌
            HktBrandInfo qf = new HktBrandInfo();
            qf.setId(brand.getId());
            HktBrandInfo find = mongoHelper.findOne(qf, HktBrandInfo.class);
            if (find == null)
                return Ret.create().setFail().set("msg", "请输入正确的品牌信息");
            if (!StringUtils.isNull(brand.getLogo())) find.setLogo(brand.getLogo());

            if (!StringUtils.isNull(brand.getStatus())) find.setStatus(brand.getStatus());
            if (!StringUtils.isNull(brand.getSort())) find.setSort(brand.getSort());
            if (!StringUtils.isNull(brand.getIsShow())) find.setIsShow(brand.getIsShow());
            if (!StringUtils.isNull(brand.getIsHot())) find.setIsHot(brand.getIsHot());
            if (!StringUtils.isNull(brand.getGoodsNum())) find.setGoodsNum(brand.getGoodsNum());
            if (!StringUtils.isNull(brand.getOrderNum())) find.setOrderNum(brand.getOrderNum());
            if (!StringUtils.isNull(brand.getTotalAmount())) find.setTotalAmount(brand.getTotalAmount());
            find.setModifyTime(DateUtil.getDateTime());
            find.setCategory_name(null);

            HktBrandCategory oldCategory = null;
            if (!StringUtils.isNull(brand.getCategoryId())) {
                if (!brand.getCategoryId().equals(find.getCategoryId())) {
                    HktBrandCategory f = new HktBrandCategory();
                    f.setId(find.getCategoryId());
                    oldCategory = brandCategoryService.queryBrandCategory(f);
                }
                find.setCategoryId(brand.getCategoryId());
            }

            HktBrand newBrand = find.toFatherBean();
            List<String> sqls = new ArrayList<>();
            sqls.add(ExecSqlLib.GetUpdateSql(newBrand));
            {
                //中文
                HktBrandDesc fd = find.getBrandDesc_ch();
                HktBrandDesc desc = brand.getBrandDesc_ch();
                if (!StringUtils.isNull(desc.getName())) {
                    fd.setName(desc.getName());
                    String name = fd.getName();
                    String py = PinyinUtil.getPinYin(name);
                    fd.setBrandHead(py.substring(0, 1).toUpperCase() + brand.getBrandDesc_en().getName().substring(0, 1).toUpperCase());
                }

                if (!StringUtils.isNull(desc.getLogoMax())) fd.setLogoMax(desc.getLogoMax());
                if (!StringUtils.isNull(desc.getVideoUrl())) fd.setVideoUrl(desc.getVideoUrl());


                if (!StringUtils.isNull(desc.getContent())) {
                    try {
                        OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
                        if (StringUtils.isNull(fd.getContent())) {
                            String url = aliyunOssService.uploadForStr(ossClient, desc.getContent(), "BrandInfo/" + System.currentTimeMillis() + ".html");
                            fd.setContent(url);
                        } else {
                            aliyunOssService.uploadForStr(ossClient, desc.getContent(), fd.getContent().substring(fd.getContent().indexOf(aliyunOSSProperties.getFileHost()) + aliyunOSSProperties.getFileHost().length() + 1));
                        }
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
                if (!StringUtils.isNull(desc.getInfo())) {
                    try {
                        OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
                        if (StringUtils.isNull(fd.getInfo())) {
                            String url = aliyunOssService.uploadForStr(ossClient, desc.getInfo(), "BrandInfo/" + System.currentTimeMillis() + ".html");
                            fd.setInfo(url);
                        } else {
                            aliyunOssService.uploadForStr(ossClient, desc.getInfo(), fd.getInfo().substring(fd.getInfo().indexOf(aliyunOSSProperties.getFileHost()) + aliyunOSSProperties.getFileHost().length() + 1));
                        }
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
                sqls.add(ExecSqlLib.GetUpdateSql(fd));

                //英文
                fd = find.getBrandDesc_en();
                desc = brand.getBrandDesc_en();
                if (!StringUtils.isNull(desc.getName())) {
                    fd.setName(desc.getName());
                    fd.setBrandHead(fd.getName().substring(0, 1).toUpperCase());
                }
                if (!StringUtils.isNull(desc.getLogoMax())) fd.setLogoMax(desc.getLogoMax());
                if (!StringUtils.isNull(desc.getVideoUrl())) fd.setVideoUrl(desc.getVideoUrl());
                if (!StringUtils.isNull(desc.getContent())) {
                    try {
                        OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
                        if (StringUtils.isNull(fd.getContent())) {
                            String url = aliyunOssService.uploadForStr(ossClient, desc.getContent(), "BrandInfo/" + System.currentTimeMillis() + ".html");
                            fd.setContent(url);
                        } else {
                            aliyunOssService.uploadForStr(ossClient, desc.getContent(), fd.getContent().substring(fd.getContent().indexOf(aliyunOSSProperties.getFileHost()) + aliyunOSSProperties.getFileHost().length() + 1));
                        }
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
                if (!StringUtils.isNull(desc.getInfo())) {
                    try {
                        OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
                        if (StringUtils.isNull(fd.getInfo())) {
                            String url = aliyunOssService.uploadForStr(ossClient, desc.getInfo(), "BrandInfo/" + System.currentTimeMillis() + ".html");
                            fd.setInfo(url);
                        } else {
                            aliyunOssService.uploadForStr(ossClient, desc.getInfo(), fd.getInfo().substring(fd.getInfo().indexOf(aliyunOSSProperties.getFileHost()) + aliyunOSSProperties.getFileHost().length() + 1));
                        }
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
                sqls.add(ExecSqlLib.GetUpdateSql(fd));
            }

            if (this.baseJdbcDao.execute(sqls)) {
                mongoHelper.removeById(find.getId(), HktBrandInfo.class);

                find.setCategory_name(category.getName());
                mongoHelper.save(find);

                if (oldCategory != null) {
                    //品牌分类管理修改
                    Integer num = oldCategory.getBrandNum();
                    if (num == null || num < 0) num = 0;
                    else if (num > 0) num -= 1;
                    oldCategory.setBrandNum(num);
                    brandCategoryService.updateBrandCetagory(opUser, oldCategory);

                    num = category.getBrandNum();
                    if (num == null) num = 1;
                    else num += 1;
                    category.setBrandNum(num);
                    brandCategoryService.updateBrandCetagory(opUser, category);
                }

                return Ret.create().setOk().set("msg", "修改品牌成功");
            } else {
                return Ret.create().setFail().set("msg", "修改品牌失败");
            }
        }
    }

    @Override
    public Ret deleteBrand(HktAdmin opUser, HktBrand brand) throws Exception {

        HktBrandInfo qf = new HktBrandInfo();
        qf.setId(brand.getId());
        HktBrandInfo find = mongoHelper.findOne(qf, HktBrandInfo.class);
        if (find == null)
            return Ret.create().setFail().set("msg", "请输入正确的品牌信息");
        if (StringUtils.isNull(find.getGoodsNum())) {
            if (find.getGoodsNum() > 0)
                return Ret.create().setFail().set("msg", "该品牌下还有商品,不可删除");
        } else {
            String sql = "select count(1) as count from hkt_goods where brand_id = '" + find.getFno() + "' ";
            int count = this.baseJdbcDao.selectCount(sql);
            if (count > 0)
                return Ret.create().setFail().set("msg", "该品牌下还有商品,不可删除");
        }
        List<String> sqls = new ArrayList<>();
        sqls.add(ExecSqlLib.GetDeleteByIdSql(brand.getId(), brand));
        sqls.add(ExecSqlLib.GetDeleteByIdSql(find.getBrandDesc_ch().getId(), find.getBrandDesc_ch()));
        sqls.add(ExecSqlLib.GetDeleteByIdSql(find.getBrandDesc_en().getId(), find.getBrandDesc_en()));

        if (this.baseJdbcDao.execute(sqls)) {

            HktBrandCategory category = new HktBrandCategory();
            category.setId(find.getCategoryId());
            category = brandCategoryService.queryBrandCategory(category);
            if (category != null) {
                Integer num = category.getBrandNum();
                if (num == null || num < 0) num = 0;
                else if (num > 0) num -= 1;
                category.setBrandNum(num);
                brandCategoryService.updateBrandCetagory(opUser, category);
            }
            mongoHelper.removeById(find.getId(), HktBrandInfo.class);


            if (!StringUtils.isNull(find.getBrandDesc_en().getInfo())) {
                OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
                aliyunOssService.deleteFile(ossClient, find.getBrandDesc_en().getInfo().substring(find.getBrandDesc_en().getInfo().indexOf(aliyunOSSProperties.getFileHost())));
            }
            if (!StringUtils.isNull(find.getBrandDesc_en().getContent())) {
                OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
                aliyunOssService.deleteFile(ossClient, find.getBrandDesc_en().getContent().substring(find.getBrandDesc_en().getContent().indexOf(aliyunOSSProperties.getFileHost())));
            }
            if (!StringUtils.isNull(find.getBrandDesc_ch().getInfo())) {
                OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
                aliyunOssService.deleteFile(ossClient, find.getBrandDesc_ch().getInfo().substring(find.getBrandDesc_ch().getInfo().indexOf(aliyunOSSProperties.getFileHost())));
            }
            if (!StringUtils.isNull(find.getBrandDesc_ch().getContent())) {
                OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
                aliyunOssService.deleteFile(ossClient, find.getBrandDesc_ch().getContent().substring(find.getBrandDesc_ch().getContent().indexOf(aliyunOSSProperties.getFileHost())));
            }


            return Ret.create().setOk().set("msg", "删除品牌成功");
        } else {
            return Ret.create().setFail().set("msg", "删除品牌失败");
        }
    }
}
