package com.org.hktshop.Dao.Impl;

import com.org.hktshop.Dao.Intface.IGoodsPageDao;
import com.org.hktshop.base.BaseDaoJdbc;
import com.org.hktshop.entity.Page;
import com.org.hktshop.entity.product.HktGoodsPageItem;
import com.org.hktshop.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


@Component
public class HktGoodsPageDao extends BaseDaoJdbc<HktGoodsPageItem> implements IGoodsPageDao {

    @Override
    public Page<HktGoodsPageItem> queryPages(HktGoodsPageItem pageItem) {

        String sql = "select DISTINCT gd.brand_id as brandId,gd.id,gd.hj_type as hjType,gd.hkt_type as hktType,gd.hkt_on_sale as hktOnSale, gd.is_sample as isSample,hgc.name_ch as typeName, gd.material_sn as materialSn, gd.ksn as k3id," +
                " hbd.name as brandName,gd.price,  gd.goods_type as goodsType," +
                " gd.is_on_sale as isOnSale,ifnull( gd.goods_img,hgc.image) as goodsImg," +
                " gd.store_count as storeCount,gd.store_company as storeCompany, gd.sales_sum as salesNum, " +
                " concat(hgc.name_ch,'  ', ds.name) as name,gd.create_time as createTime, gd.data_state as dataStatus" +
                " from hkt_goods gd left join hkt_goods_desc ds on ds.language_id = 1 and ds.goods_id = gd.ksn" +
                " left join hkt_brand_desc hbd on ds.language_id = hbd.language_id and  hbd.brand_id = gd.brand_id" +
                " left join hkt_goods_category hgc on gd.category_id = hgc.id ";
        String sql2 = "select count(1) as count " +
                " from hkt_goods gd ";
        //left join hkt_goods_desc ds on ds.language_id = 1 and ds.goods_id = gd.ksn" +
          //      " left join hkt_brand_desc hbd on ds.language_id = hbd.language_id and  hbd.brand_id = gd.brand_id" +
            //    " left join hkt_goods_category hgc on gd.category_id = hgc.id ";]
        StringBuilder sb = new StringBuilder();
        sb.append(" where 1=1 ");
//        String queySQL = " where 1 = 1 ";
        if (StringUtils.isNull(pageItem.getDataStatus())) {
            sb.append(" and gd.data_state=2 ");
//            queySQL += " and gd.data_state=2";
           /* if (!StringUtils.isNull(pageItem.getIsOnSale())) {
//                queySQL += " and gd.is_on_sale=" + pageItem.getIsOnSale();
                sb.append(" and gd.is_on_sale=").append(pageItem.getIsOnSale());
            }*/
        } else {
            if (pageItem.getDataStatus() == 0) {
                sb.append( " and gd.data_state in(1,3) ");
//                queySQL += " gd.data_state in(1,3)";
            } else {
                sb.append(" and gd.data_state=").append(pageItem.getDataStatus());
//                queySQL += " gd.data_state=" + pageItem.getDataStatus();
            }
        }

        if(!StringUtils.isNull(pageItem.getHjType()) && pageItem.getHjType() == 0 && !StringUtils.isNull(pageItem.getHktType()) && pageItem.getHktType() == 0){
            sb.append(" and gd.hj_type = 0 and gd.hkt_type = 0 ");
        }else if(!StringUtils.isNull(pageItem.getHjType()) && pageItem.getHjType() == 1 && !StringUtils.isNull(pageItem.getHktType()) && pageItem.getHktType() == 1){
            sb.append(" and (gd.hj_type = 1 or gd.hkt_type = 1) ");
        }else if(!StringUtils.isNull(pageItem.getHjType())){
            sb.append(" and gd.hj_type = ").append(pageItem.getHjType());
            if(!StringUtils.isNull(pageItem.getIsOnSale())){
                sb.append(" and gd.is_on_sale = ").append(pageItem.getIsOnSale());
            }
        }else if(!StringUtils.isNull(pageItem.getHktType())){
            sb.append(" and gd.hkt_type = ").append(pageItem.getHktType());
            if(!StringUtils.isNull(pageItem.getHktOnSale())){
                sb.append(" and gd.hkt_on_sale = ").append(pageItem.getHktOnSale());
            }
        }

        if (!StringUtils.isNull(pageItem.getTypeId())) {
            sb.append(" and gd.category_id=").append(pageItem.getTypeId());
//            if (!StringUtils.isNull(queySQL)) queySQL += " and ";
//            queySQL += " gd.category_id=" + pageItem.getTypeId();
        }
        if (!StringUtils.isNull(pageItem.getGoodsType())){
            sb.append(" and  gd.goods_type=").append(pageItem.getGoodsType());
//            if (!StringUtils.isNull(queySQL)) queySQL += " and ";
//            queySQL += " gd.goods_type=" + pageItem.getGoodsType();
        }

        if(!StringUtils.isNull(pageItem.getFno())){
            sb.append(" and gd.brand_id ='").append(pageItem.getFno()).append("'");
        }

        if (!StringUtils.isNull(pageItem.getKeyword())) {
            sb.append(" and gd.ksn like '%").append(pageItem.getKeyword()).append("%' or gd.material_sn like '%").append(pageItem.getKeyword()).append("%'");
//            if (!StringUtils.isNull(queySQL)) queySQL += " and ";
//            queySQL += " (gd.ksn like '%" + pageItem.getKeyword() + "%' or gd.material_sn like '%" + pageItem.getKeyword() + "%')";
        }

       /* if (!StringUtils.isNull(queySQL)) {
            sql += " where " + queySQL;
            sql2 += " where " + queySQL;
        }*/

       sql+=sb.toString();
       sql2+=sb.toString();

        sql +=" ORDER BY (case when gd.goods_type='2' THEN 1 else 2 END), (case when gd.brand_id='NO.0001' THEN 1 else 2 END),gd.store_count DESC,gd.sales_sum,gd.id DESC";

        return this.queryPage(sql, sql2, pageItem);
    }
}
