package com.njzscloud.supervisory.tsp.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.core.utils.GroupUtil;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.tsp.mapper.TspPhotoMapper;
import com.njzscloud.supervisory.tsp.pojo.ObtainStatusParam;
import com.njzscloud.supervisory.tsp.pojo.TspPhotoEntity;
import com.njzscloud.supervisory.tsp.pojo.TspPhotoSaveParam;
import com.njzscloud.supervisory.tsp.pojo.TspPhotoSearchParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 临时收纳点图片
 */
@Slf4j
@Service
public class TspPhotoService extends ServiceImpl<TspPhotoMapper, TspPhotoEntity> implements IService<TspPhotoEntity> {

    /**
     * 新增
     *
     * @param tspPhotoEntity 数据
     */
    public void add(TspPhotoEntity tspPhotoEntity) {
        this.save(tspPhotoEntity);
    }

    /**
     * 修改
     *
     * @param tspPhotoEntity 数据
     */
    public void modify(TspPhotoEntity tspPhotoEntity) {
        this.updateById(tspPhotoEntity);
    }

    /**
     * 删除
     *
     * @param ids Ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     *
     * @param id Id
     * @return TspPhotoEntity 结果
     */
    public TspPhotoEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     *
     * @param tspPhotoEntity 筛选条件
     * @param pageParam      分页参数
     * @return PageResult&lt;TspPhotoEntity&gt; 分页结果
     */
    public PageResult<TspPhotoEntity> paging(PageParam pageParam, TspPhotoEntity tspPhotoEntity) {
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<TspPhotoEntity>query(tspPhotoEntity)));
    }

    @Transactional(rollbackFor = Exception.class)
    public void save(TspPhotoSaveParam tspPhotoSaveParam) {
        LocalDate uploadDate = tspPhotoSaveParam.getUploadDate();
        TspPhotoEntity oldData = getOne(Wrappers.<TspPhotoEntity>query()
                .eq("tsp_id", tspPhotoSaveParam.getTspId())
                .eq("upload_date", uploadDate)
        );
        if (oldData != null) {
            this.removeById(oldData.getId());
        }
        TspPhotoEntity tspPhotoEntity = BeanUtil.copyProperties(tspPhotoSaveParam, TspPhotoEntity.class);
        this.save(tspPhotoEntity.setPhotoCount(tspPhotoEntity.getPhotos().size()));
    }

    public TspPhotoEntity listPhoto(TspPhotoSearchParam tspPhotoSearchParam) {
        return getOne(Wrappers.<TspPhotoEntity>query()
                .eq("tsp_id", tspPhotoSearchParam.getTspId())
                .eq("upload_date", tspPhotoSearchParam.getUploadDate())
        );
    }

    public Map<String, Integer> obtainStatus(ObtainStatusParam obtainStatusParam) {
        Long tspId = obtainStatusParam.getTspId();
        LocalDate uploadDate = obtainStatusParam.getUploadDate();
        int year = uploadDate.getYear();
        int month = uploadDate.getMonthValue();

        List<TspPhotoEntity> list = this.list(Wrappers.<TspPhotoEntity>query()
                .eq("tsp_id", tspId)
                .eq("YEAR(upload_date)", year)
                .eq("MONTH(upload_date)", month)
        );
        Map<Integer, Integer> map = GroupUtil.k_o(list, it -> it.getUploadDate().getDayOfMonth(), TspPhotoEntity::getPhotoCount);

        HashMap<String, Integer> res = new HashMap<>();
        DateTime now = DateTime.now();
        DateTime dateTime = DateUtil.date(uploadDate);
        for (int i = 0; i < uploadDate.lengthOfMonth(); i++) {
            if (dateTime.setField(DateField.DAY_OF_MONTH, i + 1).isAfter(now)) {
                res.put(String.valueOf(i + 1), null);
            } else {
                res.put(String.valueOf(i + 1), map.getOrDefault(i + 1, 0));
            }
        }

        return res;
    }
}
