package com.ssm.manage.service.impl;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.BaseDao;
import com.ssm.manage.dao.SsmPlatformApiDao;
import com.ssm.manage.entity.MemberAgreement;
import com.ssm.manage.entity.Platform;
import com.ssm.manage.entity.PlatformApi;
import com.ssm.manage.query.PlatformApiRequest;
import com.ssm.manage.service.SsmPlatformApiService;
import com.ssm.manage.service.SsmPlatformService;
import com.ssm.manage.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
@Slf4j
public class SsmPlatformApiServiceImp extends BaseServiceImpl<PlatformApi> implements SsmPlatformApiService {

    @Autowired
    SsmPlatformApiDao dao;

    @Override
    public BaseDao<PlatformApi> getDao() {
        return dao;
    }

    @Override
    public int add(PlatformApiRequest platformApiRequest) {
        Long id = new SnowflakeUtil(new Random().nextInt(1023)).nextId();
        platformApiRequest.setState(0);
        platformApiRequest.setAddDate(new DateTime(new Date()));
        platformApiRequest.setId(id);
        PlatformApi platformApi=new PlatformApi();
        BeanUtils.copyProperties(platformApiRequest,platformApi);
        return dao.saveNotNull(platformApi);
    }

    @Override
    public int update(PlatformApiRequest platformApiRequest) {
        PlatformApi platformApi=dao.selectByPrimaryKey(platformApiRequest.getId());

        if(platformApiRequest.getApiKey()!=null)
        platformApi.setApiKey(platformApiRequest.getApiKey());

        if(platformApiRequest.getApiSecret()!=null)
        platformApi.setApiSecret(platformApiRequest.getApiSecret());

        if(platformApiRequest.getState()!=null&&platformApiRequest.getState()!=platformApi.getState()){
            platformApi.setState(platformApiRequest.getState());
            if(platformApiRequest.getState()==1&&platformApiRequest.getOpeningYear()!=null){//审核
                platformApi.setOpeningYear(platformApiRequest.getOpeningYear());
                platformApi.setOpeningName(platformApiRequest.getUpName());
                platformApi.setOpeningDate(new DateTime(new Date()));
                platformApi.setEndDate(DateUtils.diffMonth(new DateTime(new Date()), 12*platformApiRequest.getOpeningYear()));
            }
        }
        if(platformApiRequest.getOpeningYear()!=null){
            platformApi.setOpeningYear(platformApiRequest.getOpeningYear());
            platformApi.setEndDate(DateUtils.diffMonth(platformApi.getOpeningDate(), 12*platformApiRequest.getOpeningYear()));
        }
        if(platformApiRequest.getPlatformId()!=null){
            platformApi.setPlatformId(platformApiRequest.getPlatformId());
            platformApi.setPlatformName(platformApiRequest.getPlatformName());
        }

        platformApi.setUpdateName(platformApiRequest.getUpName());
        platformApi.setUpdateDate(new DateTime(new Date()));
        return dao.updateByPrimaryKey(platformApi);
    }

    @Override
    public PageInfo<PlatformApi> selectPage(PlatformApiRequest platformApiRequest) {
        Example example = new Example(PlatformApi.class);
        Example.Criteria criteria = example.createCriteria();
        if(platformApiRequest.getState()!=null){
            criteria.andEqualTo("state",platformApiRequest.getState());
        }
        if(platformApiRequest.getStartTime()!=null&&platformApiRequest.getStartTime().trim().length()>0){
            criteria.andBetween("addDate",platformApiRequest.getStartTime(),platformApiRequest.getEndTime());
        }
        if(platformApiRequest.getStoreName()!=null&&platformApiRequest.getStoreName().trim().length()>0){
            criteria.andLike("storeName","%"+platformApiRequest.getStoreName()+"%");
        }
        if(platformApiRequest.getStoreId()!=null){
            criteria.andEqualTo("storeId",platformApiRequest.getStoreId());
        }
        PageHelper.startPage(platformApiRequest.getPageNum(),platformApiRequest.getPageSize(),platformApiRequest.getOrderAndSort());
        List<PlatformApi> lists = selectByExample(example);

        //缓存分页
        PageInfo<PlatformApi> pageCheck =new PageInfo<>(lists);
        if(lists.size()==0&&pageCheck.getTotal()>lists.size()){
            //计算页数，从最后一页查询
            Integer count=Integer.valueOf(String.valueOf(pageCheck.getTotal()));
            Integer c=count%platformApiRequest.getPageSize();
            Integer pages=1;
            if(c==0){
                pages=count/platformApiRequest.getPageSize();
            }else{
                pages=count/platformApiRequest.getPageSize()+pages;
            }
            PageHelper.startPage(pages,platformApiRequest.getPageSize(),platformApiRequest.getOrderAndSort());
            lists = selectByExample(example);
        }
        PageInfo<PlatformApi> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }
}
