package com.org.hktshop.service.product;

import com.org.hktshop.Interface.product.IHktGoodsCategoryAttrService;
import com.org.hktshop.Interface.product.IHktGoodsCategoryService;
import com.org.hktshop.base.BaseService;
import com.org.hktshop.base.MongoHelper;
import com.org.hktshop.entity.admin.HktAdmin;
import com.org.hktshop.entity.product.HktGoodsCategory;
import com.org.hktshop.entity.product.HktGoodsCategoryAttr;
import com.org.hktshop.entity.vo.HktGoodsAttrQueryItem;
import com.org.hktshop.utils.DateUtil;
import com.org.hktshop.utils.ExecSqlLib;
import com.org.hktshop.utils.Ret;
import com.org.hktshop.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/****
 * 分类属性
 * @author tw
 */
@Service
public class HktGoodsCategoryAttrServiceImpl extends BaseService<HktGoodsCategoryAttr> implements IHktGoodsCategoryAttrService {

    @Autowired
    MongoHelper<HktGoodsCategoryAttr> mongoHelper;

    @Autowired
    IHktGoodsCategoryService categoryService;

    @Autowired
    MongoHelper<HktGoodsAttrQueryItem> queryItemMongoHelper;

    /****
     * 新增/修改 商品分类属性
     * @param opUser
     * @param attr
     * @return
     */
    @Override
    public Ret updateCategoryAttr(HktAdmin opUser, HktGoodsCategoryAttr attr) {

        List<String> sqls = new ArrayList<>();
        //找到分类
        HktGoodsCategory tempCategory = new HktGoodsCategory();
        tempCategory.setId(attr.getCategoryId());
        HktGoodsCategory category = categoryService.queryCategory(tempCategory);
        if (category == null) return Ret.create().setFail().setMsg("参数错误");

        List<HktGoodsCategory> subCategories = new ArrayList<>();
        if (category.getLevel() == 1) {
            HktGoodsCategory find = new HktGoodsCategory();
            find.setParentId(attr.getCategoryId());
            subCategories = categoryService.queryLists(find);
        }

        if (StringUtils.isNull(attr.getId())) {
            //新增
            if (StringUtils.isNull(attr.getCategoryId()) || StringUtils.isNull(attr.getNameEn()) || StringUtils.isNull(attr.getNameCh()))
                return Ret.create().setFail().setMsg("参数错误");

//            if (StringUtils.isNull(attr.getIsDisplay()) || attr.getIsDisplay() != 1) attr.setIsDisplay(0);
//            if (!StringUtils.isNull(attr.getIsSpecial())) attr.setIsSpecial(attr.getIsSpecial());
            if (StringUtils.isNull(attr.getSort())) attr.setSort(50);
            attr.setCreateTime(DateUtil.getDateTime());
            sqls.add(ExecSqlLib.GetInsertSql(attr));

            if (category.getLevel() == 1) {
                for (HktGoodsCategory cate : subCategories) {
                    HktGoodsCategoryAttr goodsCategoryAttr = new HktGoodsCategoryAttr();
                    BeanUtils.copyProperties(attr, goodsCategoryAttr);
                    goodsCategoryAttr.setCategoryId(cate.getId());
                    sqls.add(ExecSqlLib.GetInsertSql(goodsCategoryAttr));
                }
            }
        } else {
            //修改
            HktGoodsCategoryAttr f = new HktGoodsCategoryAttr();
            f.setId(attr.getId());
//            f = mongoHelper.findOne(f, HktGoodsCategoryAttr.class);
            f = this.baseJdbcDao.query(ExecSqlLib.GetQuerySql(f),f);
            if (f == null) Ret.create().setFail().setMsg("参数错误");

            HktGoodsCategoryAttr last = new HktGoodsCategoryAttr();
            BeanUtils.copyProperties(f, last);

            if (!StringUtils.isNull(attr.getNameCh())) f.setNameCh(attr.getNameCh());
            if (!StringUtils.isNull(attr.getNameEn())) f.setNameEn(attr.getNameEn());
//            if (!StringUtils.isNull(attr.getIsDisplay())) f.setIsDisplay(attr.getIsDisplay());
            if (!StringUtils.isNull(attr.getSort())) f.setSort(attr.getSort());
//            if (!StringUtils.isNull(attr.getIsSpecial())) f.setIsSpecial(attr.getIsSpecial());
            if (!StringUtils.isNull(attr.getIsSelected())) f.setIsSelected(attr.getIsSelected());
            f.setModifyTime(DateUtil.getDateTime());
            sqls.add(ExecSqlLib.GetUpdateSql(f));

            if (category.getLevel() == 1) {
                for (HktGoodsCategory cate : subCategories) {
                    HktGoodsCategoryAttr tempAttr = new HktGoodsCategoryAttr();
                    tempAttr.setCategoryId(cate.getId());
                    tempAttr.setNameCh(last.getNameCh());
                    HktGoodsCategoryAttr goodsCategoryAttr = queryCategoryAttr(tempAttr);
                    if (goodsCategoryAttr == null) continue;

                    if (!StringUtils.isNull(attr.getNameCh())) goodsCategoryAttr.setNameCh(attr.getNameCh());
                    if (!StringUtils.isNull(attr.getNameEn())) goodsCategoryAttr.setNameEn(attr.getNameEn());
//                    if (!StringUtils.isNull(attr.getIsDisplay())) goodsCategoryAttr.setIsDisplay(attr.getIsDisplay());
                    if (!StringUtils.isNull(attr.getSort())) goodsCategoryAttr.setSort(attr.getSort());
//                    if (!StringUtils.isNull(attr.getIsSpecial())) goodsCategoryAttr.setIsSpecial(attr.getIsSpecial());
                    if (!StringUtils.isNull(attr.getIsSelected()))
                        goodsCategoryAttr.setIsSelected(attr.getIsSelected());
                    goodsCategoryAttr.setModifyTime(DateUtil.getDateTime());
                    sqls.add(ExecSqlLib.GetUpdateSql(goodsCategoryAttr));
                }
            }
        }



        if (this.baseJdbcDao.execute(sqls)) {
           /* if(!StringUtils.isNull(attr.getId()) && !StringUtils.isNull(attr.getSort())){
                Criteria criteria = Criteria.where("_id").is(attr.getId() + "-1");
                criteria.and("typeTwoId").is(attr.getCategoryId());
                Query query = new Query(criteria);
                HktGoodsAttrQueryItem one = queryItemMongoHelper.findOne(query, HktGoodsAttrQueryItem.class);
                one.setSort(attr.getSort());
                queryItemMongoHelper.mongoUpdate(one.getId(),one,HktGoodsAttrQueryItem.class);
            }

            subCategories.add(category);
            for (HktGoodsCategory c : subCategories) {
                Query query = new Query();
                query.addCriteria(Criteria.where("categoryId").is(c.getId()));
                mongoHelper.delete(query, HktGoodsCategoryAttr.class);

                HktGoodsCategoryAttr attrt = new HktGoodsCategoryAttr();
                attrt.setCategoryId(c.getId());
                queryLists(attrt);
            }*/
            //todo 商品分类属性修改成功后，如很更新商品的属性
            return Ret.create().setOk().setMsg("修改成功");
        }

        return Ret.create().setFail().setMsg("修改失败");
    }

    @Override
    public HktGoodsCategoryAttr queryCategoryAttr(HktGoodsCategoryAttr categoryAttr) {
//        return mongoHelper.findOne(categoryAttr, HktGoodsCategoryAttr.class);
        return this.baseJdbcDao.query(ExecSqlLib.GetQuerySql(categoryAttr),categoryAttr);
    }

    @Override
    public List<HktGoodsCategoryAttr> queryLists(HktGoodsCategoryAttr categoryAttr) {

//        List<HktGoodsCategoryAttr> list = mongoHelper.find(categoryAttr, HktGoodsCategoryAttr.class);
        List<HktGoodsCategoryAttr> list = this.baseJdbcDao.queryList(ExecSqlLib.GetQuerySql(categoryAttr).replace(" order by id"," order by sort asc"),categoryAttr);
        if (list.isEmpty()) {
            list = queryList(categoryAttr);
            for (HktGoodsCategoryAttr attr : list) {
                mongoHelper.save(attr);
            }
        }
        return list;
    }

    /*****
     * 删除商品分类属性
     * @param opUser
     * @param categoryAttr
     * @return
     */
    @Override
    public Ret deleteCategoryAttr(HktAdmin opUser, HktGoodsCategoryAttr categoryAttr) {
        if (StringUtils.isNull(categoryAttr.getId())) return Ret.create().setFail().setMsg("参数错误");

        HktGoodsCategoryAttr attr = new HktGoodsCategoryAttr();
        attr.setId(categoryAttr.getId());

        if (this.baseJdbcDao.execute(ExecSqlLib.GetDeleteByIdSql(categoryAttr.getId(), categoryAttr))) {
            mongoHelper.removeById(categoryAttr.getId(), HktGoodsCategoryAttr.class);
            return Ret.create().setOk().setMsg("删除成功");
        } else {
            return Ret.create().setOk().setMsg("删除失败");
        }
    }

    @Override
    public HktGoodsCategoryAttr findById(HktGoodsCategoryAttr hktGoodsCategoryAttr) {
        String sql = ExecSqlLib.GetQuerySql(hktGoodsCategoryAttr);
        return this.baseJdbcDao.query(sql,hktGoodsCategoryAttr);
    }

    @Override
    public List<HktGoodsCategoryAttr> queryListByCategoryId(HktGoodsCategoryAttr hca) {
        String sql = ExecSqlLib.GetQuerySql(hca);
        return this.baseJdbcDao.queryList(sql,hca);
    }
}
