package com.ssm.manage.service.impl;

import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.SsmMemberCsDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.query.MemberCsRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.*;
import com.ssm.manage.utils.Constant;
import com.ssm.manage.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
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.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
@Slf4j
public class SsmMemberCsServiceImpl extends BaseServiceImpl<MemberCs> implements SsmMemberCsService {

    @Autowired
    private SsmSysBusinessParaService ssmSysBusinessParaService;

    @Autowired
    private SsmMemberCsDao dao;

    @Override
    public SsmMemberCsDao getDao() {
        return dao;
    }

    @Override
    public List<MemberCs> selectRecentRecord(MemberCsRequest memberCsRequest) throws SmsException {
        Example example = new Example(MemberCs.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != memberCsRequest.getMemberId()){
            criteria.andEqualTo("memberId", memberCsRequest.getMemberId());
        }
        if (null != memberCsRequest.getStoreId()){
            criteria.andEqualTo("storeId", memberCsRequest.getStoreId());
        }
        if (null != memberCsRequest.getDocId()){
            criteria.andEqualTo("docId", memberCsRequest.getDocId());
        }
        if (null != memberCsRequest.getState()) {
            criteria.andEqualTo("state", memberCsRequest.getState());
        }
        //获取评价天数
        SysBusinessParameters sysBusinessParameters = new SysBusinessParameters();
        sysBusinessParameters.setParametersName("会员评价天数");
        List<SysBusinessParameters> parameters = ssmSysBusinessParaService.select(sysBusinessParameters);
        int days = Integer.valueOf(parameters.get(0).getParametersValues());
        Date startTime = null;
        try {
            //获取三天前的时间 并将时间时分秒后缀换算成00:00:00 在转换成时间类型进行比较
            startTime = DateUtils.ymdhmsString2DateTime(DateUtils.addDateBeginPostfix(DateUtils.ymdFormat(DateUtils.diffDay(new Date(), -days))));
        } catch (ParseException e) {
            throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
        }
        criteria.andBetween("docDate",startTime,new Date());
        List<MemberCs> list = dao.selectByExample(example);
        return list;
    }

    @Override
    public int addMemberCs(MemberCsRequest memberCsRequest) {
        MemberCs memberCs = new MemberCs();
        BeanUtils.copyProperties(memberCsRequest, memberCs);
        memberCs.setState(0);
        memberCs.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
//        if (memberCs.getDocType().equals(Constant.doc_type_store_cash)){
//            CashList cashList = ssmCashService.s
//        }
        return dao.saveNotNull(memberCs);
    }

    @Override
    public int appraise(MemberCsRequest memberCsRequest) {
        MemberCs memberCs = new MemberCs();
        BeanUtils.copyProperties(memberCsRequest, memberCs);
        memberCs.setState(1);
        return dao.updateByPrimaryKeySelective(memberCs);
    }

    @Override
    public List<MemberCs> selectMemberCsByStaff(MemberCsRequest memberCsRequest) throws SmsException {
        Example example = new Example(MemberCs.class);
        Example.Criteria criteria = example.createCriteria();

        if (null != memberCsRequest.getStoreId()){
            criteria.andEqualTo("storeId", memberCsRequest.getStoreId());
        }
        if (null != memberCsRequest.getStaffIds() && StringUtils.isNotBlank(memberCsRequest.getStaffIds())){
            criteria.andLike("staffIds", "%" + memberCsRequest.getStaffIds() + "%");
        }
        Date startTime = null;
        Date endTime = null;
        try {

            startTime = DateUtils.ymdhmsString2DateTime(DateUtils.addDateBeginPostfix(DateUtils.ymdFormat(memberCsRequest.getDocDate())));
            endTime = DateUtils.ymdhmsString2DateTime(DateUtils.addDateEndPostfix(DateUtils.ymdFormat(memberCsRequest.getDocDate())));
        } catch (ParseException e) {
            throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
        }
        criteria.andBetween("docDate",startTime,endTime);
        criteria.andEqualTo("state",memberCsRequest.getState());
        List<MemberCs> list = dao.selectByExample(example);
        return list;
    }
}
