package com.yunyan.business.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.json.JSONUtil;
import com.yunyan.business.document.y22.pojo.finalExcelY22.FinalExcelDataVo;
import com.yunyan.business.document.y23.budget.UpdateReportBudgetData;
import com.yunyan.business.document.y23.budget.pojo.budgetY23.BudgetP3ChangeItemVo;
import com.yunyan.business.document.y23.budget.pojo.budgetY23.purchase.BudgetP3p10p2ItemVo;
import com.yunyan.common.utils.DateUtils;
import com.yunyan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yunyan.business.mapper.PurchaseDataMapper;
import com.yunyan.business.domain.PurchaseData;
import com.yunyan.business.service.IPurchaseDataService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Builder;
import com.yunyan.common.core.domain.AjaxResultVo;
import org.springframework.beans.BeanUtils;
import com.yunyan.common.core.domain.model.LoginUser;
import com.yunyan.common.utils.SecurityUtils;
import com.yunyan.common.enums.DataContentType;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;
import com.yunyan.common.annotation.DataScope;

/**
 * 预算政府采购数据Service业务层处理
 *
 * @author yunyan
 * @date 2024-02-27
 */
@Slf4j
@Service
public class PurchaseDataServiceImpl extends ServiceImpl<PurchaseDataMapper, PurchaseData> implements IPurchaseDataService
{
    @Autowired
    private PurchaseDataMapper purchaseDataMapper;

    /**
     * 查询预算政府采购数据列表
     *
     * @param purchaseData 预算政府采购数据
     * @return 预算政府采购数据
     */    @Override
    public List<PurchaseData> selectPurchaseDataList(PurchaseData purchaseData)
    {
        return purchaseDataMapper.selectPurchaseDataList(purchaseData);
    }

    /**
    * 新增
    * @param
    * @return
    */
    @Override
    public AjaxResultVo add(PurchaseData dto) {
        PurchaseData entity = dto;

        this.save(entity);
        return AjaxResultVo.success();
    }

    /**
     * 编辑
     * @param
     * @return
     */
    @Override
    public AjaxResultVo edit(PurchaseData dto) {

        PurchaseData entity = this.getById(dto.getId());
        if(null == entity) {
            return AjaxResultVo.error("非法操作");
        }

        entity = new PurchaseData();
        BeanUtils.copyProperties(dto, entity);

        this.updateById(entity);
        return AjaxResultVo.success();
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public AjaxResultVo info(Long id) {
        PurchaseData entity = this.getById(id);
        if(null == entity) {
            return AjaxResultVo.error("数据查询异常~");
        }

        return AjaxResultVo.success(entity);
    }

    @Override
    public boolean initLastValue(String area, String year) {

        String lastYear = String.valueOf(Integer.valueOf(year) - 1);

        PurchaseData data = this.lambdaQuery().eq(PurchaseData::getYear, year).eq(PurchaseData::getArea, area).last("and rownum = 1 order by create_time desc").one();

        PurchaseData dataLast = this.lambdaQuery().eq(PurchaseData::getYear, lastYear).eq(PurchaseData::getArea, area).last("and rownum = 1 order by create_time desc").one();

        if(null == data) {
            log.debug("未查询到本年的数据");
            return false;
        }

        if(null == dataLast) {
            log.debug("未查询到去年的数据");
            return false;
        }

        //json数据
        List<BudgetP3p10p2ItemVo> dataList = JSONUtil.toList(data.getJsonContent(), BudgetP3p10p2ItemVo.class);
        //json数据 Last
        List<BudgetP3p10p2ItemVo> dataLastList = JSONUtil.toList(dataLast.getJsonContent(), BudgetP3p10p2ItemVo.class);

        UpdateReportBudgetData.setPurchaseLastValue(dataList, dataLastList);


        String jsonTableContent = JSONUtil.toJsonStr(dataList);

        PurchaseData updateEntity = new PurchaseData();
        updateEntity.setId(data.getId());
        updateEntity.setJsonContent(jsonTableContent);
        this.updateById(updateEntity);

        return true;
    }

    @Override
    public List<BudgetP3p10p2ItemVo> getPurchaseDataList(String area, String year) {
        PurchaseData data = this.lambdaQuery().eq(PurchaseData::getYear, year).eq(PurchaseData::getArea, area).last("and rownum = 1 order by create_time desc").one();
        List<BudgetP3p10p2ItemVo> dataList = new ArrayList<>();
        if(null != data && StringUtils.isNotBlank(data.getJsonContent())) {
            dataList = JSONUtil.toList(data.getJsonContent(), BudgetP3p10p2ItemVo.class);
        }

        return dataList;
    }
}
