package com.org.hktshop.service.System.advert;

import com.org.hktshop.Interface.advert.IHktAdCategoryService;
import com.org.hktshop.Interface.advert.IHktAdDetailService;
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.advert.HktAdCategory;
import com.org.hktshop.entity.advert.HktAdDetail;
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.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.util.List;
import java.util.Vector;

/***
 * 广告详情
 * @author tw
 */
@Service
public class HktAdDetialServiceImpl extends BaseService<HktAdDetail> implements IHktAdDetailService {

    @Autowired
    IHktAdCategoryService categoryService;


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

    @Override
    public void InitData() {
        List<HktAdDetail> list = mongoHelper.find(new HktAdDetail(), HktAdDetail.class);

        if (list.isEmpty()) {
            list = queryList(new HktAdDetail());
            for (HktAdDetail detail : list) {
                mongoHelper.save(detail);
            }
        }
    }

    @Override
    public List<HktAdDetail> queryLists(HktAdDetail detail) {
        Criteria criteria = new Criteria();
        Criteria criteria1 = Criteria.where("aid").is(detail.getAid());
        Criteria criteria2;
        if(!StringUtils.isNull(detail.getSysType()) && detail.getSysType() == 2){
            criteria2 = Criteria.where("sysType").is(2);
        }else {
            criteria2 = Criteria.where("sysType").is(1);
        }
        criteria.andOperator(criteria1,criteria2);
        Query query = new Query(criteria);
        query.with(new Sort(Sort.Direction.ASC, "sort"));
//        List<HktAdDetail> list = mongoHelper.find(detail, HktAdDetail.class);
        return mongoHelper.find(query, HktAdDetail.class);
    }

    @Override
    public Page<HktAdDetail> queryPage(HktAdDetail detail) {
//        Page<HktAdDetail> page = mongoHelper.findPage(detail, HktAdDetail.class);
        String sql = ExecSqlLib.GetQuerySql(detail);
        Page<HktAdDetail> page = this.baseJdbcDao.queryPage(sql, detail);
        List<HktAdCategory> categories = categoryService.queryLists(new HktAdCategory());

        for (HktAdDetail detail1 : page.getRecords()) {
            for (HktAdCategory c : categories) {
                if (c.getId().equals(detail1.getAid())) {
                    detail1.setTypeName(c.getName());
                    break;
                }
            }
        }
        return page;
    }

    @Override
    public HktAdDetail queryInfo(HktAdDetail detail) {
        return mongoHelper.findOne(detail, HktAdDetail.class);
    }

    @Override
    public Ret updateAd(HktAdmin admin, HktAdDetail detail) {
        if (StringUtils.isNull(detail.getId())) {

            HktAdCategory category = new HktAdCategory();
            category.setId(detail.getAid() + "");
            category = categoryService.queryCategory(category);
            if (category == null) return Ret.create().setFail().setMsg("提交参数不够完整");

            if (StringUtils.isNull(detail.getTitle())
                    || StringUtils.isNull(detail.getStime())
                    || StringUtils.isNull(detail.getEtime())
                    || StringUtils.isNull(detail.getUrl())
//                    || StringUtils.isNull(detail.getAd_type())
                    || StringUtils.isNull(detail.getImg())) {
                return Ret.create().setFail().setMsg("提交参数不够完整");
            }

            detail.setCreateTime(DateUtil.getDateTime());

            if (this.updateJdbc(detail)) {
                mongoHelper.delete(new Query(), HktAdDetail.class);
                InitData();
                return Ret.create().setOk().setMsg("提交成功");
            } else {
                return Ret.create().setFail().setMsg("提交失败");
            }
        } else {

            detail.setModifyTime(DateUtil.getDateTime());

            if (this.updateJdbc(detail)) {
                HktAdDetail find = new HktAdDetail();
                find.setId(detail.getId());
                String sql = ExecSqlLib.GetQuerySql(find);
                HktAdDetail query = this.baseJdbcDao.query(sql, find);
                if("0".equals(query.getStatus())){
                    mongoHelper.removeById(find.getId(), HktAdDetail.class);
                }else {
                    mongoHelper.removeById(find.getId(), HktAdDetail.class);
                    mongoHelper.save(query);
                }
                return Ret.create().setOk().setMsg("提交成功");
            } else {
                return Ret.create().setFail().setMsg("提交失败");
            }
        }
    }

    @Override
    public Ret deleteAd(HktAdmin admin, HktAdDetail detail) {
        HktAdDetail find = new HktAdDetail();
        find.setId(detail.getId());
        find = mongoHelper.findOne(find, HktAdDetail.class);
        if (find == null) return Ret.create().setFail().setMsg("提交参数不够完整");

        if (this.deleteById(find.getId(), find)) {

            mongoHelper.removeById(find.getId(), HktAdDetail.class);
            return Ret.create().setOk().setMsg("删除成功");
        }
        return Ret.create().setFail().setMsg("删除失败");
    }

    public static void main(String[] ar) {
        int num = getNum();
        System.out.println(num);

    }

      public static int getNum(){
          int a = 1;
          try {
              a = 2;
              int i = a / 0;
          }catch (Exception e){
              a = 3;
          }finally {
              a = 4;
          }
          return a;
      }
}
