package com.ssm.manage.dao.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.impl.BaseDaoImpl;
import com.ssm.manage.dao.SsmRechargeTypeDao;
import com.ssm.manage.entity.BaseStores;
import com.ssm.manage.entity.PromotionProject;
import com.ssm.manage.entity.RechargeType;
import com.ssm.manage.entity.RechargeTypeStore;
import com.ssm.manage.mapper.RechargeTypeMapper;
import com.ssm.manage.query.RechargeTypeRequest;
import com.ssm.manage.query.StoreRequest;
import com.ssm.manage.service.SsmBaseStoreService;
import com.ssm.manage.service.SsmPromotionProjectService;
import com.ssm.manage.service.SsmRechargeTypeStoreService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class SsmRechargeTypeDaoImpl extends BaseDaoImpl<RechargeType> implements SsmRechargeTypeDao {
    @Autowired
    private RechargeTypeMapper mapper;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;
    @Autowired
    private SsmRechargeTypeStoreService ssmRechargeTypeStoreService;

    @Autowired
    private SsmPromotionProjectService ssmPromotionProjectService;

    @Override
    public RechargeTypeMapper getMapper() {
        return mapper;
    }

    @Override
    public PageInfo<RechargeType> selectPage(RechargeTypeRequest rechargeRequest) {
        StoreRequest storeRequest = new StoreRequest();
        storeRequest.setId(rechargeRequest.getStoreId());
        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
        for (BaseStores pagesorganization : pagesorganizations) {
            if(null == pagesorganization.getParentId()){
                rechargeRequest.setStoreId(pagesorganization.getId());
            }
        }

        Example example = new Example(RechargeType.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("recharge_type_num desc");
        if (StringUtils.isNotBlank(rechargeRequest.getRechargeTypeName())){
            criteria.andLike("rechargeTypeName","%" + rechargeRequest.getRechargeTypeName() + "%");
        }
        if (StringUtils.isNotBlank(rechargeRequest.getRechargeTypeNum())){
            criteria.andLike("rechargeTypeNum","%" + rechargeRequest.getRechargeTypeNum() + "%");
        }
        if (rechargeRequest.getState() != null){
            criteria.andEqualTo("state",rechargeRequest.getState());
        }
        List<Integer> objects = new ArrayList<>();
        objects.add(0);
        objects.add(1);
        //充值类别的促销卡
//        objects.add(2);

        criteria.andEqualTo("storeId",rechargeRequest.getStoreId());
        criteria.andEqualTo("cardType",0);
        criteria.andIn("type",objects);

        PageHelper.startPage(rechargeRequest.getPageNum(),rechargeRequest.getPageSize(),"discount");
        List<RechargeType> list = selectByExample(example);
        PageInfo<RechargeType> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


    @Override
    public PageInfo<RechargeType> selectPagess(RechargeTypeRequest rechargeRequest) {
        Example example = new Example(RechargeType.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("recharge_type_num desc");
        if (StringUtils.isNotBlank(rechargeRequest.getRechargeTypeName())){
            criteria.andLike("rechargeTypeName","%" + rechargeRequest.getRechargeTypeName() + "%");
        }
        if (rechargeRequest.getState() != null){
            criteria.andEqualTo("state",rechargeRequest.getState());
        }
        if (rechargeRequest.getLiftType() != null){
            criteria.andEqualTo("liftType",rechargeRequest.getLiftType());
        }
        criteria.andEqualTo("storeId",rechargeRequest.getStoreId());
        criteria.andEqualTo("cardType",1);
        criteria.andEqualTo("liftCardId",rechargeRequest.getLiftCardId());

        PageHelper.startPage(rechargeRequest.getPageNum(),rechargeRequest.getPageSize());
        List<RechargeType> list = selectByExample(example);
        PageInfo<RechargeType> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<RechargeType> selectsjianjiList(RechargeTypeRequest rechargeRequest) {
        Example example = new Example(RechargeType.class);
        Example.Criteria criteria = example.createCriteria();
        if (rechargeRequest.getState() != null){
            criteria.andEqualTo("state",1);
        }
        if (rechargeRequest.getType() != null){
            criteria.andEqualTo("type",rechargeRequest.getType());
        }
        if (rechargeRequest.getId() != null){
            criteria.andNotEqualTo("id",rechargeRequest.getId());
        }
        criteria.andEqualTo("storeId",rechargeRequest.getStoreId());

        criteria.andEqualTo("cardType",0);

        //PageHelper.startPage(rechargeRequest.getPageNum(),rechargeRequest.getPageSize());
        List<RechargeType> list = selectByExample(example);
        return list;
    }

    @Override
    public List<RechargeType> selectsjianjiLists(RechargeTypeRequest rechargeRequest) {
        Example example = new Example(RechargeType.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("state",1);
        if (rechargeRequest.getType() != null){
            criteria.andEqualTo("type",rechargeRequest.getType());
        }
//        if (rechargeRequest.getStoreId() != null){
//            criteria.andEqualTo("storeId",rechargeRequest.getStoreId());
//        }
        //criteria.andEqualTo("cardType",1);
        //criteria.andEqualTo("liftCardId",rechargeRequest.getId());
        if (rechargeRequest.getLiftCardId() != null){
            criteria.andEqualTo("liftCardId",rechargeRequest.getLiftCardId());
        }
        if (rechargeRequest.getLiftType() != null){
            criteria.andEqualTo("liftType",rechargeRequest.getLiftType());
        }
        //PageHelper.startPage(rechargeRequest.getPageNum(),rechargeRequest.getPageSize());
        List<RechargeType> list = selectByExample(example);
        return list;
    }

    @Override
    public PageInfo<RechargeType> selectnewPage(RechargeTypeRequest rechargeRequest) {
        rechargeRequest.setType(2);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(new Date().getTime());
        rechargeRequest.setStartDates(dateString);
        List<RechargeType> rechargeTypes = mapper.selectRechange(rechargeRequest);
        PageHelper.startPage(rechargeRequest.getPageNum(),rechargeRequest.getPageSize());
        PageInfo<RechargeType> pageInfo = new PageInfo<>(rechargeTypes);
        return pageInfo;
    }

    @Override
    public PageInfo<RechargeType> selectnewPages(RechargeTypeRequest rechargeRequest) {
        StoreRequest storeRequest = new StoreRequest();
        storeRequest.setId(rechargeRequest.getStoreId());
        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
        for (BaseStores pagesorganization : pagesorganizations) {
            if(null == pagesorganization.getParentId()){
                rechargeRequest.setStoreId(pagesorganization.getId());
            }
        }
        rechargeRequest.setType(2);
        PageHelper.startPage(rechargeRequest.getPageNum(),rechargeRequest.getPageSize());
        List<RechargeType> rechargeTypes = mapper.selectRechange(rechargeRequest);
        for(RechargeType rechargeType:rechargeTypes){
            if(rechargeType.getStoreType()==1){
                List<RechargeTypeStore> storeList=ssmRechargeTypeStoreService.selectStore(rechargeType);
                rechargeType.setRechargeTypeStoreList(storeList);
            }
            rechargeType.setPromotionProjectList(new ArrayList<>());
            //添加送疗程列表
            if(rechargeType.getType()==2){
                //判断有没有
                PromotionProject promotionProject = new PromotionProject();
                promotionProject.setPromotionId(rechargeType.getId());
                List<PromotionProject> select = ssmPromotionProjectService.select(promotionProject);
                rechargeType.setPromotionProjectList(select);
            }
        }

        PageInfo<RechargeType> pageInfo = new PageInfo<>(rechargeTypes);
        return pageInfo;
    }

    @Override
    public PageInfo<RechargeType> selectpromotionPage(RechargeTypeRequest rechargeRequest) {
        Example example = new Example(RechargeType.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("recharge_type_num desc");
        if (StringUtils.isNotBlank(rechargeRequest.getRechargeTypeName())){
            criteria.andLike("rechargeTypeName","%" + rechargeRequest.getRechargeTypeName() + "%");
        }
        if (rechargeRequest.getState() != null){
            criteria.andEqualTo("state",rechargeRequest.getState());
        }
        rechargeRequest.setType(3);
        criteria.andEqualTo("type",rechargeRequest.getType());

        criteria.andEqualTo("storeId",rechargeRequest.getStoreId());

        criteria.andEqualTo("cardType",0);

        PageHelper.startPage(rechargeRequest.getPageNum(),rechargeRequest.getPageSize());
        List<RechargeType> list = selectByExample(example);
        PageInfo<RechargeType> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public int insertBatch(List<RechargeType> list) {
        return mapper.insertBatch(list);
    }
}
