package com.org.hktshop.service.product;

import com.org.hktshop.Interface.product.IHktGoodsSeoDescService;
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.product.HktGoodsDesc;
import com.org.hktshop.entity.product.HktGoodsSeoDesc;
import com.org.hktshop.utils.ExecSqlLib;
import com.org.hktshop.utils.Ret;
import com.org.hktshop.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

@Service
public class IHktGoodsSeoDescServiceImpl extends BaseService<HktGoodsSeoDesc> implements IHktGoodsSeoDescService {

    MongoHelper<HktGoodsSeoDesc> mongoHelper = new MongoHelper<>();

    @Override
    public List<HktGoodsSeoDesc> queryLists(HktGoodsSeoDesc desc) {

        List<HktGoodsSeoDesc> list = mongoHelper.find(desc, HktGoodsSeoDesc.class);
        if (list.isEmpty()) {
            for (HktGoodsSeoDesc d : list) {
                mongoHelper.save(d);
            }
        }
        return list;
    }

    @Override
    public HktGoodsSeoDesc queryDes(HktGoodsSeoDesc desc) {
        HktGoodsSeoDesc dd = mongoHelper.findOne(desc, HktGoodsSeoDesc.class);
        return dd;
    }

    @Override
    public Ret updateDesc(HktAdmin admin, HktGoodsSeoDesc desc) {
        if (StringUtils.isNull(desc.getId())) {
            if (this.updateJdbc(desc)) {
                List<HktGoodsSeoDesc> seoDesc = queryList(desc);
                for (HktGoodsSeoDesc d : seoDesc) {
                    mongoHelper.save(d);
                }
                return Ret.create().setOk();
            } else {
                return Ret.create().setFail().setMsg("新增失敗");
            }
        } else {

            HktGoodsSeoDesc d = new HktGoodsSeoDesc();
            d.setId(desc.getId());
            HktGoodsSeoDesc dd = mongoHelper.findOne(d, HktGoodsSeoDesc.class);
            if (dd == null) return Ret.create().setFail().setMsg("修改失败");

            if (!StringUtils.isNull(desc.getLanguageId())) dd.setLanguageId(desc.getLanguageId());
            if (!StringUtils.isNull(desc.getUseType())) dd.setUseType(desc.getUseType());
            if (!StringUtils.isNull(desc.getName())) dd.setName(desc.getName());
            if (!StringUtils.isNull(desc.getSort())) dd.setSort(desc.getSort());

            if (this.updateJdbc(dd)) {
                mongoHelper.mongoUpdate(desc.getId(), desc, HktGoodsSeoDesc.class);
                return Ret.create().setOk();
            } else {
                return Ret.create().setFail().setMsg("修改失敗");
            }
        }
    }

    @Override
    public Ret deleteDesc(HktAdmin admin, HktGoodsSeoDesc desc) {
        HktGoodsSeoDesc d = new HktGoodsSeoDesc();
        d.setId(desc.getId());
        HktGoodsSeoDesc dd = mongoHelper.findOne(d, HktGoodsSeoDesc.class);
        if (dd == null) return Ret.create().setFail().setMsg("删除失败");

        if (this.deleteById(desc.getId(), desc)) {
            mongoHelper.removeById(desc.getId(), HktGoodsSeoDesc.class);
            return Ret.create().setOk();
        } else {
            return Ret.create().setFail().setMsg("删除失败");
        }
    }

    @Override
    public List<HktGoodsSeoDesc> selectLists(List<HktGoodsDesc> descs) {
        StringBuffer stringBuffer = new StringBuffer();
        String sql = "select id,language_id as languageId,name,use_type as useType from hkt_goods_seo_desc where id in(";
        stringBuffer.append(sql);
        for(HktGoodsDesc hgd : descs){
//            stringBuffer.append(hgd.getKeywordsId()+",");
            stringBuffer.append(hgd.getIndustryId()+",");
            stringBuffer.append(hgd.getTechnologyId()+",");
        }
        String str = stringBuffer.toString();
        String sqls = str.substring(0, str.length()-1) + ")";
        List<HktGoodsSeoDesc> hktGoodsSeoDescs = this.baseJdbcDao.queryList(sqls, new HktGoodsSeoDesc());
        return hktGoodsSeoDescs;
    }

    @Override
    public Page<HktGoodsSeoDesc> queryByPage(HktGoodsSeoDesc hktGoodsSeoDesc) {
        String sql = ExecSqlLib.GetQuerySql(hktGoodsSeoDesc);
        Page<HktGoodsSeoDesc> hktGoodsSeoDescPage = this.baseJdbcDao.queryPage(sql, hktGoodsSeoDesc);
        return hktGoodsSeoDescPage;
    }

    @Override
    public HktGoodsSeoDesc findTitleById(HktGoodsSeoDesc hktGoodsSeoDesc) {
        String sql = ExecSqlLib.GetQuerySql(hktGoodsSeoDesc);
        HktGoodsSeoDesc query = this.baseJdbcDao.query(sql, hktGoodsSeoDesc);
        return query;
    }

    @Override
    public void initDate() {
        HktGoodsSeoDesc hktGoodsSeoDesc = new HktGoodsSeoDesc();
        hktGoodsSeoDesc.setUseType(6);
        List<HktGoodsSeoDesc> list = mongoHelper.find(hktGoodsSeoDesc, HktGoodsSeoDesc.class);
        if(CollectionUtils.isEmpty(list)){
            String sql = ExecSqlLib.GetQuerySql(hktGoodsSeoDesc);
            List<HktGoodsSeoDesc> hktGoodsSeoDescs = this.baseJdbcDao.queryList(sql, hktGoodsSeoDesc);
            for(HktGoodsSeoDesc hd : hktGoodsSeoDescs){
                mongoHelper.save(hd);
            }
        }
    }
}
