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.SsmMemberAgreementDao;
import com.ssm.manage.dao.SsmMemberFileDao;
import com.ssm.manage.dao.impl.SsmMemberFileDaoImpl;
import com.ssm.manage.entity.*;
import com.ssm.manage.mapper.SsmMemberAgreementMapper;
import com.ssm.manage.page.BasePage;
import com.ssm.manage.query.MemberAgreementRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmMemberAgreementService;
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 SsmMemberAgreementImpl extends BaseServiceImpl<MemberAgreement> implements SsmMemberAgreementService {
    @Autowired
    private SsmMemberAgreementDao dao;
    @Autowired
    private SsmMemberFileDaoImpl ssmMemberFileDao;

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

    @Override
    public int addMemberFile(MemberAgreementRequest memberFileRequest) throws SmsException {
        int i = 0;
        MemberAgreement memberAgreement=new MemberAgreement();
        BeanUtils.copyProperties(memberFileRequest, memberAgreement);
        Long id = new SnowflakeUtil(new Random().nextInt(1023)).nextId();
        memberAgreement.setId(id);
        memberAgreement.setAddDate(new DateTime(new Date()));
        memberAgreement.setStartState(1);
        if(memberFileRequest.getMemberAgreementFiles()!=null&&memberFileRequest.getMemberAgreementFiles().size()>0){
            for(MemberAgreementFile memberAgreementFile:memberFileRequest.getMemberAgreementFiles()){
                Long memberAgreementFileId = new SnowflakeUtil(new Random().nextInt(1023)).nextId();
                memberAgreementFile.setId(memberAgreementFileId);
                memberAgreementFile.setMemberFileId(id);
                ssmMemberFileDao.insert(memberAgreementFile);
            }
        }
        try {
            this.end(memberAgreement);
            i=dao.saveNotNull(memberAgreement);
        }catch (Exception e){
            e.printStackTrace();
        }

        return i;
    }

    @Override
    public PageInfo<MemberAgreement> selectPage(MemberAgreementRequest memberFileRequest) {
        Example example = new Example(MemberAgreement.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("storeId",memberFileRequest.getStoreId());
        if(memberFileRequest.getAddName()!=null&&memberFileRequest.getAddName().trim().length()>0){
            criteria.andLike("addName","%"+memberFileRequest.getAddName()+"%");
        }
        if(memberFileRequest.getStartTime()!=null&&memberFileRequest.getStartTime().trim().length()>0){
            criteria.andBetween("addDate",memberFileRequest.getStartTime(),memberFileRequest.getEndTime());
        }
        PageHelper.startPage(memberFileRequest.getPageNum(),memberFileRequest.getPageSize(),"start_state  desc,add_date  desc");
        List<MemberAgreement> lists = selectByExample(example);

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

        //lists.remove(lists.get(0));
        PageInfo<MemberAgreement> pageInfo = new PageInfo<>(lists);
        //pageInfo.setTotal(pageInfo.getTotal()-1);
        return pageInfo;
    }

    @Override
    public MemberAgreement selectOne(MemberAgreementRequest memberFileRequest) {
        Example example = new Example(MemberAgreement.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("storeId",memberFileRequest.getStoreId());
        PageHelper.startPage(memberFileRequest.getPageNum(),memberFileRequest.getPageSize(),memberFileRequest.getOrderAndSort());
        List<MemberAgreement> lists = selectByExample(example);
        return lists.get(0);
    }
    @Autowired
    private SsmMemberAgreementMapper mapper;

    @Override
    public int updateState() {
        return mapper.updateState();
    }

    @Override
    public int updateStateByStartState(MemberAgreement memberAgreement) {
        return mapper.updateStateByStartState(memberAgreement);
    }

    @Override
    public MemberAgreement selectByMember() {

        return mapper.selectByMember();
    }


    //禁用协议
    public void end(MemberAgreement memberAgreement) throws SmsException {
        int i=0;
        //查出当前门店启用协议的版本
        Example example = new Example(MemberAgreement.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("storeId",memberAgreement.getStoreId());
        criteria.andEqualTo("startState",1);
        List<MemberAgreement> lists = selectByExample(example);
        MemberAgreement memberAgreement1=lists.get(0);
        //停掉当前启用的协议版本
        memberAgreement1.setStartState(0);
        memberAgreement1.setEndName(memberAgreement.getAddName());
        memberAgreement1.setEndDate(new DateTime(new Date()));
        i=dao.updateByPrimaryKey(memberAgreement1);
        if(i!=1)
           throw new SmsException(ResponseCode.MSG,"禁用旧版本协议失败");
    }


}