package cn.iocoder.yudao.module.bpm.api.oa;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.replacementCard.ReplacementCardItemVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAReplacementCardDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAReplacementCardService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@RestController
@Validated
public class BpmOAReplacementCardApiImpl implements BpmOAReplacementCardApi{

    @Resource
    private BpmOAReplacementCardService replacementCardService;

    @Override
    public CommonResult<List<Long>> getPunchRecordIds() {
        List<BpmOAReplacementCardDO> list = replacementCardService.getListByResult();
        
        List<Long> punchRecordIds = new ArrayList<>();
        for (BpmOAReplacementCardDO item : list) {

            List<ReplacementCardItemVO> respVO = item.getApplicationItem();
            //直接从数据库取出来的List<Reimbursement> 实际上是List<LinkedHashMap>类型 所以不能直接遍历
            //将list再次转为json串，然后由json串再转为list
            String json = JsonUtils.toJsonString(respVO);
            respVO = JsonUtils.parseArray(json, ReplacementCardItemVO.class);
            punchRecordIds.addAll(convertList(respVO, ReplacementCardItemVO::getPunchRecordId));
        }
        return success(punchRecordIds);
    }
}
