UserServiceImpl.java 5.31 KB
package com.server.web.common.service.Impl;

import com.server.utils.DateUtils;
import com.server.utils.ResultMapUtil;
import com.server.web.common.mapper.TKzyUserIntegralFlowingMapper;
import com.server.web.common.mapper.TKzyUserMapper;
import com.server.web.common.mapping.IntegralMapper;
import com.server.web.common.model.TKzyUser;
import com.server.web.common.model.TKzyUserIntegralFlowing;
import com.server.web.common.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by 豆腐干Sama on 2018/12/6.
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private TKzyUserMapper tKzyUserMapper;
    @Autowired
    private TKzyUserIntegralFlowingMapper tKzyUserIntegralFlowingMapper;
    @Autowired
    private IntegralMapper integralMapper;

    @Override
    public void save(TKzyUser user) {

    }

    public static void main(String[] args) {
        Date last = DateUtils.firstDayOfMonth();
        System.out.println(last);

    }

    @Override
    public Map signIn(TKzyUser loginUser) {
        if (loginUser == null || loginUser.getId() == null) {
            return ResultMapUtil.returnMap("0", "请先登录!", null);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (loginUser.getLastSignDt() != null && simpleDateFormat.format(loginUser.getLastSignDt()).equals(simpleDateFormat.format(new Date()))) {
            return ResultMapUtil.returnMap("0", "您已签到!", null);
        }

        TKzyUserIntegralFlowing tKzyUserIntegralFlowing = new TKzyUserIntegralFlowing();
        tKzyUserIntegralFlowing.setCreateDt(new Date());
        tKzyUserIntegralFlowing.setSourceType(1);
        tKzyUserIntegralFlowing.setType(1);
        tKzyUserIntegralFlowing.setUserId(loginUser.getId());
        if (loginUser.getIntegral() == null) {
            loginUser.setIntegral(0);
        }
        if (loginUser.getSignInCount() == null) {
            loginUser.setSignInCount(0);
        }
        String firstDay = simpleDateFormat.format(DateUtils.firstDayOfMonth());
        Integer integral = 1;
        if (firstDay.equals(simpleDateFormat.format(new Date()))) {
            loginUser.setSignInCount(1);
            tKzyUserIntegralFlowing.setIntegral(1);
        } else {
            if (loginUser.getSignInCount() <= 2) {
                integral = 1;
            } else if (loginUser.getSignInCount() == 3) {
                integral = 3;
            } else if (loginUser.getSignInCount() == 4) {
                integral = 1;
            } else if (loginUser.getSignInCount() == 5) {
                integral = 5;
                tKzyUserIntegralFlowing.setIntegral(5);
            } else {
                integral = 1;
            }
            loginUser.setSignInCount(loginUser.getSignInCount() + 1);
        }
        loginUser.setIntegral(loginUser.getIntegral() + integral);
        tKzyUserIntegralFlowing.setIntegral(integral);
        loginUser.setLastSignDt(new Date());
        tKzyUserMapper.updateByPrimaryKeySelective(loginUser);
        tKzyUserIntegralFlowingMapper.insertSelective(tKzyUserIntegralFlowing);

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("integral", integral);
        dataMap.put("signInCount", loginUser.getSignInCount());
        return ResultMapUtil.returnMap("1", "操作成功!", dataMap);
    }

    @Override
    public Map<String, Object> signInList(TKzyUser loginUser) {
        //List<String> dayList = getDayListOfMonth();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("userId", loginUser.getId());
        param.put("month", simpleDateFormat.format(new Date()));
        List<Map<String, Object>> list = integralMapper.signInList(param);
        List<String> hasSignDay = new ArrayList<>();
        for (Map<String, Object> map : list) {
            String dd = (String) map.get("days");
            String[] _D = dd.split("/");
            String ymd = _D[0] + "/" + (_D[1].startsWith("0") ? (_D[1].substring(1, _D[1].length())) : _D[1]) + "/" + (_D[2].startsWith("0") ? (_D[2].substring(0, _D[2].length())) : _D[2]);
            hasSignDay.add(ymd);
        }

        /*List<Map<String, Object>> daysList = new ArrayList<>();
        Map<String, Object> flag = null;
        for (String everyDay : dayList) {
            flag = new HashMap<>();
            flag.put("day", everyDay);
            if (hasSignDay.contains(everyDay)) {
                flag.put("signFlag", 1);
            } else {
                flag.put("signFlag", 0);
            }
            daysList.add(flag);
        }*/


        Map<String, Object> param2 = new HashMap<String, Object>();
        param2.put("userId", loginUser.getId());
        param2.put("today", simpleDateFormat2.format(new Date()));

        Map<String, Object> todaySign = integralMapper.queryTodaySign(param2);
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("list", list);
        dataMap.put("todaySign", todaySign);
        dataMap.put("daysList", hasSignDay);
        return ResultMapUtil.returnMap("1", "", dataMap);
    }


}