package thyyxxk.webserver.service.executeItem;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.executeItem.YzExecuteSignDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.datamodify.YzActOrder;
import thyyxxk.webserver.entity.executeItem.YzChangeParam;
import thyyxxk.webserver.utils.AssertUtil;
import thyyxxk.webserver.utils.CommonUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.TokenUtil;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author:lihong
 * @Date: 2023/11/8
 */
@Service
@Slf4j
public class YzExecuteSignService {
    @Autowired
    private YzExecuteSignDao dao;


    public IPage queryYzSign(YzChangeParam param) {
        IPage page = new Page(param.getCurrentPage(),param.getPageSize());
        List<YzActOrder> list = dao.selectYzSign(param);
        Long total = dao.selectYzSignTotal(param);
        CommonUtil.BeanTrim(list);
        page.setRecords(list);
        page.setTotal(total);
        return page;
    }
    @Transactional(rollbackFor = Exception.class)
    public ResultVo<String> saveYzSign(YzChangeParam param) {
        AssertUtil.isnotBlank(param.getYzActOrders(),"保存数据不能为空");
        String userId =  TokenUtil.getInstance().getTokenUserId();
        List<YzActOrder> collect1 = param.getYzActOrders().stream().filter(o -> StrUtil.isNotBlank(o.getExecId())).collect(Collectors.toList());
        AssertUtil.isnotBlank(collect1,"保存数据不能为空");
        List<BigDecimal> collect = collect1.stream().map(item -> item.getActOrderNo()).collect(Collectors.toList());
        Date date = collect1.stream().map(item -> item.getExecTime()).collect(Collectors.toList()).get(0);
        String actOrderNos = CollUtil.join(collect, ",");
        dao.updateExcute(userId, DateUtil.formatDateTime(date),actOrderNos);
        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE,"保存成功");
    }
}
