package com.mortals.xhx.module.stat.service.impl;
import com.mortals.framework.common.Rest;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.module.ph.model.PhQueueStatEntity;
import com.mortals.xhx.module.ph.model.PhQueueStatQuery;
import com.mortals.xhx.module.ph.service.PhQueueStatService;
import com.mortals.xhx.module.pj.model.PjEvaluateEntity;
import com.mortals.xhx.module.pj.model.PjEvaluateQuery;
import com.mortals.xhx.module.pj.model.PjEvaluateStatEntity;
import com.mortals.xhx.module.pj.model.PjEvaluateStatQuery;
import com.mortals.xhx.module.pj.service.PjEvaluateStatService;
import com.mortals.xhx.module.stat.model.StatQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.stat.dao.StatDao;
import com.mortals.xhx.module.stat.model.StatEntity;
import com.mortals.xhx.module.stat.service.StatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
* StatService
* 汇总统计 service实现
*
* @author zxfei
* @date 2024-07-02
*/
@Service("statService")
@Slf4j
public class StatServiceImpl extends AbstractCRUDServiceImpl<StatDao, StatEntity, Long> implements StatService {

    @Autowired
    private PjEvaluateStatService pjEvaluateStatService;
    @Autowired
    private PhQueueStatService phQueueStatService;


    @Override
    public Result<StatEntity> find(StatEntity entity, PageInfo pageInfo, Context context) throws AppException {
        Result<StatEntity> result = new Result<>();
        StatQuery query = new StatQuery();
        BeanUtils.copyProperties(entity, query, BeanUtil.getNullPropertyNames(entity));
        List<StatEntity> billInfos = this.getBillInfos(query, pageInfo, context);
        result.setList(billInfos);
        result.setPageInfo(pageInfo);
        return result;
    }


    public List<StatEntity> getBillInfos(StatQuery query, PageInfo pageInfo, Context context) {
        if (!ObjectUtils.isEmpty(query.getGroupList())) {
            //针对groupList分组 进行排序
            if (query.getGroupList().contains("year") && query.getGroupList().contains("month") && query.getGroupList().contains("day")) {
                query.setOrderColList(Arrays.asList(new OrderCol("year", OrderCol.DESCENDING), new OrderCol("month", OrderCol.DESCENDING), new OrderCol("day", OrderCol.DESCENDING), new OrderCol("createTime")));
            } else if (query.getGroupList().contains("year") && query.getGroupList().contains("month")) {
                query.setOrderColList(Arrays.asList(new OrderCol("year", OrderCol.DESCENDING), new OrderCol("month", OrderCol.DESCENDING), new OrderCol("createTime")));
            } else if (query.getGroupList().contains("year")) {
                query.setOrderColList(Arrays.asList(new OrderCol("year", OrderCol.DESCENDING), new OrderCol("createTime")));
            }
        }
        return this.getDao().getStatList(query, pageInfo);
    }

    @Override
    public Rest<Void> updateSiteStat(StatEntity entity, Context context) {
        //查询当前站点当日评价数量
        PjEvaluateStatQuery pjEvaluateStatQuery = new PjEvaluateStatQuery();
        pjEvaluateStatQuery.setSiteId(entity.getSiteId());
        pjEvaluateStatQuery.setYear(entity.getYear());
        pjEvaluateStatQuery.setMonth(entity.getMonth());
        pjEvaluateStatQuery.setDay(entity.getDay());
        PjEvaluateStatEntity pjEvaluateStatEntity = pjEvaluateStatService.selectOne(pjEvaluateStatQuery, context);

        PhQueueStatQuery phQueueStatQuery = new PhQueueStatQuery();
        phQueueStatQuery.setSiteId(entity.getSiteId());
        phQueueStatQuery.setYear(entity.getYear());
        phQueueStatQuery.setMonth(entity.getMonth());
        phQueueStatQuery.setDay(entity.getDay());

        PhQueueStatEntity phQueueStatEntity = phQueueStatService.selectOne(phQueueStatQuery, context);

        int pjcount = pjEvaluateStatEntity == null ? 0 : pjEvaluateStatEntity.getPjCount();
        int phcount = phQueueStatEntity == null ? 0 : phQueueStatEntity.getPhCount();

        StatEntity StatEntity = this.selectOne(new StatQuery()
                .siteId(entity.getSiteId())
                .year(entity.getYear())
                .month(entity.getMonth())
                .day(entity.getDay()));
        if (ObjectUtils.isEmpty(StatEntity)) {
            StatEntity = new StatEntity();
            StatEntity.initAttrValue();
            StatEntity.setSiteId(entity.getSiteId());
            StatEntity.setSiteCode(entity.getSiteCode());
            StatEntity.setSiteName(entity.getSiteName());
            StatEntity.setCount(pjcount+phcount);
            StatEntity.setYear(entity.getYear());
            StatEntity.setMonth(entity.getMonth());
            StatEntity.setDay(entity.getDay());
            StatEntity.setCreateTime(new Date());
            StatEntity.setCreateUserId(1L);
            this.save(StatEntity);
        } else {
            StatEntity.setCount(pjcount+phcount);
            StatEntity.setYear(entity.getYear());
            StatEntity.setMonth(entity.getMonth());
            StatEntity.setDay(entity.getDay());
            StatEntity.setUpdateTime(new Date());
            StatEntity.setUpdateUserId(1L);
            this.update(StatEntity);
        }
        return Rest.ok();
    }
}