package com.hzs.member.achieve.controller.api;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.enums.EPlaceDept;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.achieve.controller.vo.CuActiveMemberCensusVO;
import com.hzs.member.achieve.service.ICuActiveMemberCensusService;
import com.hzs.member.achieve.service.ICuMemberAchieveService;
import com.hzs.member.achieve.vo.CuMemberNewAddAchieveVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 统计活跃会员 前端控制器
 * </p>
 *
 * @author zhangjing
 * @since 2023-05-17
 */
@RestController
@RequestMapping("/api/active-member-census")
public class ApiCuActiveMemberCensusController extends BaseController {

    @Autowired
    private ICuActiveMemberCensusService cuActiveMemberCensusService;
    @Autowired
    private ICuMemberAchieveService cuMemberAchieveService;

    /**
     * @description: 查询活跃用户
     * @author: zhang jing
     * @date: 2023/5/18 9:57
     * @param: []
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @GetMapping("/activeMember")
    public AjaxResult activeMember(CuActiveMemberCensusVO cvo) {
        //查找安置人伞下
        Long memberId = SecurityUtils.getUserId();
        LocalDate endDate = LocalDate.now();
        String endDateStr = endDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        cvo.setEndDate(endDateStr);
        LocalDate startDate = cuMemberAchieveService.getStartDateByType(cvo.getDateType(), null);
        String startDateStr = startDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        cvo.setStartDate(startDateStr);
        cvo.setPkMember(memberId);
        //相差天数
        Period between = Period.between(startDate, endDate);
        //相差天数
        int days = between.getDays();
        int months = between.getMonths();
        if (months == 1) {
            days = 30;
        }

        List<CuActiveMemberCensusVO> list = cuActiveMemberCensusService.queryActiveMember(cvo);
        List<CuActiveMemberCensusVO> calist=new ArrayList<>();
        if (CollectionUtil.isEmpty(list)) {
            for (int i = 0; i < days; i++) {
                CuActiveMemberCensusVO ca = new CuActiveMemberCensusVO();
                ca.setCreationTime(DateUtils.toDate(startDate.plusDays(i)));
                calist.add(ca);
            }
        } else {
            for (int i = 0; i < days; i++) {
                Date creationTime =  DateUtils.toDate(startDate.plusDays(i));
                String compareTime =  DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,DateUtils.toDate(startDate.plusDays(i)));
                CuActiveMemberCensusVO cam = new CuActiveMemberCensusVO();
                cam.setCreationTime(creationTime);
                for(CuActiveMemberCensusVO ca:list){
                    if(compareTime.equals(ca.getCompareTime())){
                        if(ca.getPlaceDept().intValue()==EPlaceDept.LEFT_DEPT.getValue()){
                            cam.setLeftCoun(ca.getTotalCount());
                        }
                        if(ca.getPlaceDept().intValue()==EPlaceDept.RIGHT_DEPT.getValue()){
                            cam.setRightCount(ca.getTotalCount());
                        }
                    }
                }
                calist.add(cam);
            }
        }
        //排序
        calist.sort(Comparator.comparing(CuActiveMemberCensusVO::getCreationTime));
        return AjaxResult.success(calist);
    }


}
