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.SsmStoreMemberDao;
import com.ssm.manage.entity.StoreMemberList;
import com.ssm.manage.entity.StoreMemberListUpdate;
import com.ssm.manage.extendEntity.StoreDaily;
import com.ssm.manage.mapper.StoreMemberListMapper;
import com.ssm.manage.mapper.StoreMemberListUpdateMapper;
import com.ssm.manage.query.MemberRequest;
import com.ssm.manage.query.StoreDailyRequest;
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.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class SsmStoreMemberDaoImpl extends BaseDaoImpl<StoreMemberList> implements SsmStoreMemberDao {
    @Autowired
    private StoreMemberListMapper mapper;

    @Autowired
    private StoreMemberListUpdateMapper storeMemberListUpdateMapper;

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

    @Override
    public PageInfo<StoreMemberList> selectPage(MemberRequest memberRequest) {
        Example example = new Example(StoreMemberList.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("member_num desc");
        if (memberRequest.getState() != null){
            criteria.andEqualTo("state",memberRequest.getState());
        }
        if (memberRequest.getAssignState() != null){
            criteria.andEqualTo("assignState",memberRequest.getAssignState());
        }
        if (memberRequest.getMemberName() != null && StringUtils.isNotBlank(memberRequest.getMemberName()) &&!memberRequest.getMemberName().trim().equals("")){
            criteria.andLike("memberName","%" + memberRequest.getMemberName() + "%").orLike("memberNum","%"+memberRequest.getMemberName()+"%");
        }
        if (memberRequest.getMobilePhone() != null && StringUtils.isNotBlank(memberRequest.getMobilePhone())){
            criteria.andEqualTo("mobilePhone", memberRequest.getMobilePhone());
        }
        if (memberRequest.getDateStr() != null && StringUtils.isNotBlank(memberRequest.getDateStr())){
            criteria.andLike("inputDate", memberRequest.getDateStr() + "%");
        }

//        if (null != memberRequest.getStoreId()) {
//            criteria.andEqualTo("storeId", memberRequest.getStoreId());
//        }
        if (null != memberRequest.getConnStoreId()) {
            criteria.andEqualTo("connStoreId", memberRequest.getConnStoreId());
        }

        if (null != memberRequest.getStoresId()&&memberRequest.getStoresId().size()>0) {
            criteria.andIn("connStoreId", memberRequest.getStoresId());
        }
//        if (null != memberRequest.getStoresId()){
//            criteria.andIn("storeId", memberRequest.getStoresId());
//        }


        //111
        if(memberRequest.getSlectPhoneName()!=null){
            Example.Criteria criteria2 = example.createCriteria();
            criteria2.orLike("mobilePhone","%"+memberRequest.getSlectPhoneName()+"%");
            criteria2.orLike("memberName","%"+memberRequest.getSlectPhoneName()+"%");
            example.and(criteria2);
        }

        PageHelper.startPage(memberRequest.getPageNum(),memberRequest.getPageSize(),memberRequest.getOrderAndSort());
        List<StoreMemberList> lists = selectByExample(example);
        PageInfo<StoreMemberList> pagechek= new PageInfo<>(lists);
        if(lists.size()==0&&pagechek.getTotal()>lists.size()){
            //计算页数
            Integer count=Integer.valueOf(String.valueOf(pagechek.getTotal()));
            Integer c=count%memberRequest.getPageSize();
            Integer pages=1;
            if(c==0){
                pages=count/memberRequest.getPageSize();
            }else{
                pages=count/memberRequest.getPageSize()+pages;
            }
             PageHelper.startPage(pages,memberRequest.getPageSize(),memberRequest.getOrderAndSort());
            lists  = selectByExample(example);
        }
        PageInfo<StoreMemberList> pageInfo = new PageInfo<>(lists);
        //判断缓存查询多页
        PageInfo<StoreMemberList> chek = new PageInfo<>(lists);
        if(chek.getTotal()>chek.getPageSize()&&memberRequest.getMemberName()!=null&&StringUtils.isNotBlank(memberRequest.getMemberName()) &&!memberRequest.getMemberName().trim().equals("")){
            pageInfo.setPageSize(Integer.valueOf(String.valueOf(chek.getTotal())));
        }
        return pageInfo;
    }

    @Override
    public PageInfo<StoreMemberList> selectPageassign(MemberRequest memberRequest) {
        Example example = new Example(StoreMemberList.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("member_num desc");
        if (memberRequest.getState() != null){
            criteria.andEqualTo("state",memberRequest.getState());
        }
        if (memberRequest.getAssignState() != null){
            criteria.andEqualTo("assignState",memberRequest.getAssignState());
        }
        if (memberRequest.getMemberName() != null && StringUtils.isNotBlank(memberRequest.getMemberName())){
            criteria.andLike("memberName","%" + memberRequest.getMemberName() + "%");
        }
        if (memberRequest.getMobilePhone() != null && StringUtils.isNotBlank(memberRequest.getMobilePhone())){
            criteria.andEqualTo("mobilePhone", memberRequest.getMobilePhone());
        }
        if (memberRequest.getDateStr() != null && StringUtils.isNotBlank(memberRequest.getDateStr())){
            criteria.andLike("inputDate", memberRequest.getDateStr() + "%");
        }

        if (null != memberRequest.getStoreId()) {
            criteria.andEqualTo("connStoreId", memberRequest.getStoreId());
        }
        if (null != memberRequest.getConnStoreId()) {
            criteria.andEqualTo("connStoreId", memberRequest.getConnStoreId());
        }
//        if (null != memberRequest.getStoresId()){
//            criteria.andIn("storeId", memberRequest.getConnStoreId());
//        }
        PageHelper.startPage(memberRequest.getPageNum(),memberRequest.getPageSize(),memberRequest.getOrderAndSort());
        List<StoreMemberList> lists = selectByExample(example);
        PageInfo<StoreMemberList> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }

    @Override
    public StoreDaily selectDayRecord(StoreDailyRequest storeDailyRequest) {
        return mapper.selectDayRecord(storeDailyRequest);
    }

    @Override
    public List<StoreMemberList> selectPhone(String phone) {
        return mapper.selectPhone(phone);
    }

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

    @Override
    public PageInfo<StoreMemberListUpdate> memberUpdatePage(MemberRequest memberRequest) {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-6);
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
//        String timeStr = format.format(calendar.getTime());
        //查询最近6个月
        memberRequest.setEndDate(calendar.getTime());
        PageHelper.startPage(memberRequest.getPageNum(),memberRequest.getPageSize(),memberRequest.getOrderAndSort());
        List<StoreMemberListUpdate> lists = storeMemberListUpdateMapper.memberUpdatePage(memberRequest);

        PageInfo<StoreMemberListUpdate> pageInfo = new PageInfo<>(lists);
        //判断缓存查询多页
        PageInfo<StoreMemberListUpdate> chek = new PageInfo<>(lists);
        if(chek.getTotal()>chek.getPageSize()&&memberRequest.getMemberName()!=null
                &&StringUtils.isNotBlank(memberRequest.getMemberName()) &&!memberRequest.getMemberName().trim().equals("")){
            pageInfo.setPageSize(Integer.valueOf(String.valueOf(chek.getTotal())));
        }
        return pageInfo;
    }

}
