package com.hzs.member.census.controller.api;/**
 * @Description:
 * @Author: yuhui
 * @Time: 2023/4/27 17:05
 * @Classname: CensusController
 * @PackageName: com.hzs.member.census.controller.api
 */

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.member.census.CuRecommendTimeCensus;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.census.service.ICuRecommendTimeCensusService;
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;


/**
 *@BelongsProject: hzs_cloud
 *@BelongsPackage: com.hzs.member.census.controller.api
 *@Author: yh
 *@CreateTime: 2023-04-27  17:05
 *@Description: 用户首页统计controller
 *@Version: 1.0
 */
@RestController
@RequestMapping("/api/census")
public class CensusController extends BaseController {

    @Autowired
    private ICuRecommendTimeCensusService  recommendTimeCensusService;


    /**
     * 获取用户推荐平均用时
     * @return
     */
    @GetMapping("get-member-recommend-time")
    public AjaxResult getMemberRecommendTime(){

        QueryWrapper<CuRecommendTimeCensus> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TIME", DateUtil.format(DateUtils.currentDate(),DateUtils.YYYY_MM_DD));
        queryWrapper.eq("PK_MEMBER", SecurityUtils.getUserId());
        CuRecommendTimeCensus recommendTimeCensus = recommendTimeCensusService.getOne(queryWrapper);
        if (recommendTimeCensus == null){
            recommendTimeCensus = new CuRecommendTimeCensus();
        }
        // 天转化成秒 返回
        BigDecimal time = new BigDecimal(60 * 60 * 24);
        if (recommendTimeCensus.getThisTime() != null){
            BigDecimal thisTime = new BigDecimal(recommendTimeCensus.getThisTime()).multiply(time);
            recommendTimeCensus.setThisTime(thisTime.toString());
        }
        if (recommendTimeCensus.getTeamTime() != null){
            BigDecimal teamTime = new BigDecimal(recommendTimeCensus.getTeamTime()).multiply(time);
            recommendTimeCensus.setTeamTime(teamTime.toString());
        }
        if (recommendTimeCensus.getCompanyTime() != null){
            BigDecimal companyTime = new BigDecimal(recommendTimeCensus.getCompanyTime()).multiply(time);
            recommendTimeCensus.setCompanyTime(companyTime.toString());
        }
        if (recommendTimeCensus.getStarTime() != null){
            BigDecimal startTime = new BigDecimal(recommendTimeCensus.getStarTime()).multiply(time);
            recommendTimeCensus.setStarTime(startTime.toString());
        }

        return AjaxResult.success(recommendTimeCensus);
    }

    /**
     * 将小数转换成时间
     * @param second
     * @return
     */
    public static String secondToDate(double second) {
        Long time =  new Long(new Double(second).longValue());
        String strTime = null;
        Long days = time / (60 * 60 * 24);
        Long hours = (time % (60 * 60 * 24)) / (60 * 60);
        Long minutes = (time % (60 * 60)) / 60;
        Long seconds = time % 60;
        if (days > 0) {
            strTime = days + "天" + hours + "小时" + minutes + "分钟";
        } else if (hours > 0) {
            strTime = hours + "小时" + minutes + "分钟";
        } else if (minutes > 0) {
            strTime = minutes + "分钟" + seconds + "秒";
        } else {
            strTime = second + "秒";
        }
        return strTime;
    }


}
