package com.org.hktshop.service.product;

import com.org.hktshop.Interface.product.IHktGoodsDeliveryService;
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.HktGoods;
import com.org.hktshop.entity.product.HktGoodsDelivery;
import com.org.hktshop.entity.product.HktGoodsDesc;
import com.org.hktshop.utils.ExecSqlLib;
import com.org.hktshop.utils.Ret;
import com.org.hktshop.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

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

@Service
public class HktGoodsDeliveryServiceImpl extends BaseService<HktGoodsDelivery> implements IHktGoodsDeliveryService {

    @Autowired
    MongoHelper<HktGoodsDelivery> mongoHelper;

    @Override
    public void InitDate() {
        List<HktGoodsDelivery> list = mongoHelper.find(new HktGoodsDelivery(), HktGoodsDelivery.class);
        if (list.isEmpty()) {
            list = queryList(new HktGoodsDelivery());
            for (HktGoodsDelivery delivery : list) {
                mongoHelper.save(delivery);
            }
        }
    }

    @Override
    public Ret updateDelivery(HktAdmin opUser, HktGoodsDelivery delivery) throws Exception {
        if (StringUtils.isNull(delivery.getId())) {

            HktGoodsDelivery find = new HktGoodsDelivery();
            find.setFno(delivery.getFno());
            find = this.query(find);
            if (find != null)
                return Ret.create().setFail().setMsg("编码已存在");

            HktGoodsDelivery oldDelivery = null;
            List<String> sqls = new ArrayList<>();
            if (1 == delivery.getIsDefault()) {
                HktGoodsDelivery df = new HktGoodsDelivery();
                df.setLanguageId(delivery.getLanguageId());
                df.setIsDefault(1);
                oldDelivery = query(df);
                if (oldDelivery != null) {
                    oldDelivery.setIsDefault(0);
                    sqls.add(ExecSqlLib.GetUpdateSql(oldDelivery));
                }
            } else {
                delivery.setIsDefault(0);
            }

            sqls.add(ExecSqlLib.GetInsertSql(delivery));
            if (this.baseJdbcDao.executeTransaction(sqls)) {
                find = this.baseJdbcDao.query(ExecSqlLib.GetQuerySql(delivery), delivery);
                mongoHelper.save(find);
                if (oldDelivery != null)
                    mongoHelper.mongoUpdate(oldDelivery.getId(), oldDelivery, HktGoodsDelivery.class);

                return Ret.create().setOk();
            } else {
                return Ret.create().setFail().setMsg("新增失败");
            }
        } else {

            HktGoodsDelivery f = new HktGoodsDelivery();
            f.setId(delivery.getId());
            HktGoodsDelivery find = mongoHelper.findOne(f, HktGoodsDelivery.class);
            if (find == null) return Ret.create().setFail().setMsg("参数错误");

            HktGoodsDelivery oldDelivery = null;
            List<String> sqls = new ArrayList<>();
            if (!StringUtils.isNull(delivery.getIsDefault()) && 1 == delivery.getIsDefault()) {
                HktGoodsDelivery df = new HktGoodsDelivery();
                df.setLanguageId(delivery.getLanguageId());
                df.setIsDefault(1);
                oldDelivery = query(df);

                if (oldDelivery != null) {
                    if (oldDelivery.getFno().equals(find.getFno())) {
                        find.setIsDefault(1);
                        oldDelivery = null;
                    } else {
                        oldDelivery.setIsDefault(0);
                        sqls.add(ExecSqlLib.GetUpdateSql(oldDelivery));
                        sqls.add(ExecSqlLib.GetUpdateSql(oldDelivery));
                    }
                } else {
                    find.setIsDefault(1);
                }
            }

            if (!StringUtils.isNull(delivery.getLanguageId())) find.setLanguageId(delivery.getLanguageId());
            if (!StringUtils.isNull(delivery.getLdata())) find.setLdata(delivery.getLdata());
            if (!StringUtils.isNull(delivery.getRdata())) find.setRdata(delivery.getRdata());
            if (!StringUtils.isNull(delivery.getUnit())) find.setUnit(delivery.getUnit());
            if (!StringUtils.isNull(delivery.getLocked())) find.setLocked(delivery.getLocked());
            if (!StringUtils.isNull(delivery.getInfo())) find.setInfo(delivery.getInfo());

            sqls.add(ExecSqlLib.GetUpdateSql(find));
            if (this.baseJdbcDao.executeTransaction(sqls)) {
                mongoHelper.mongoUpdate(find.getId(), find, HktGoodsDelivery.class);
                if (oldDelivery != null)
                    mongoHelper.mongoUpdate(oldDelivery.getId(), oldDelivery, HktGoodsDelivery.class);

                return Ret.create().setOk();
            } else {
                return Ret.create().setFail().setMsg("修改成功");
            }
        }
    }

    @Override
    public Ret deleteDelivery(HktAdmin opUser, HktGoodsDelivery delivery) {

        HktGoodsDelivery goodsDelivery = new HktGoodsDelivery();
        goodsDelivery.setId(delivery.getId());
        HktGoodsDelivery find = mongoHelper.findOne(goodsDelivery, HktGoodsDelivery.class);
        if (find == null) return Ret.create().setFail().setMsg("参数错误");
        if (find.getLocked() == 1) return Ret.create().setFail().setMsg("已锁定，不可删除");

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

    @Override
    public List<HktGoodsDelivery> queryLists(HktGoodsDelivery delivery) {
        Query query = mongoHelper.createQuery(delivery, HktGoodsDelivery.class);
        query.with(new Sort(Sort.Direction.ASC, "_id"));
        return mongoHelper.find(query, HktGoodsDelivery.class);
    }

    @Override
    public Page<HktGoodsDelivery> queryPage(HktGoodsDelivery delivery) {

        Query query = mongoHelper.createQuery(delivery, HktGoodsDelivery.class);
        query.with(new Sort(Sort.Direction.ASC, "_id"));

        return mongoHelper.findPage(delivery, query, HktGoodsDelivery.class);
    }

    @Override
    public HktGoodsDelivery query(HktGoodsDelivery delivery) {
        return mongoHelper.findOne(delivery, HktGoodsDelivery.class);
    }

    @Override
    public List<HktGoodsDelivery> queryListByDataSource(HktGoodsDelivery delivery) {
        String sql = ExecSqlLib.GetQuerySql(delivery);
        List<HktGoodsDelivery> hktGoodsDeliveries = this.baseJdbcDao.queryList(sql, delivery);
        return hktGoodsDeliveries;
    }
}
