package com.bonus.material.clz.controller;

import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.material.basic.domain.BmProject;
import com.bonus.material.clz.domain.vo.MaterialSltAgreementInfo;
import com.bonus.material.clz.domain.vo.sltAgreementInfo.MaterialSltInfoVo;
import com.bonus.material.clz.service.ClzSltAgreementInfoService;
import com.bonus.material.common.domain.dto.SelectDto;
import com.bonus.material.settlement.domain.SltAgreementReduce;
import com.bonus.material.settlement.domain.SltAgreementRelation;
import com.bonus.material.task.domain.TmTask;
import com.bonus.material.task.mapper.TmTaskMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author ma_sh
 * @create 2025/6/27 15:18
 */
@Api(tags = "材料站结算申请接口")
@RestController
@RequestMapping("/material_sltAgreementInfo")
@Slf4j
public class ClzSltAgreementInfoController extends BaseController {

    @Resource
    private ClzSltAgreementInfoService clzSltAgreementInfoService;

    @Resource
    private TmTaskMapper taskMapper;

    /**
     * 根据条件获取协议结算列表
     */
    @ApiOperation(value = "根据条件获取协议结算列表")
    @GetMapping("/getSltAgreementInfo4Project")
    public TableDataInfo getSltAgreementInfo4Project(MaterialSltAgreementInfo bean) {
        startPage();
        List<MaterialSltAgreementInfo> list = clzSltAgreementInfoService.getSltAgreementInfo4Project(bean);
        return getDataTable(list);
    }

    @ApiOperation(value = "工程下拉选根据材料站班组查询")
    @PostMapping("getProjectListByUnitIds")
    public AjaxResult getProjectListByUnitIds(@RequestBody BmProject bmProject) {
        return clzSltAgreementInfoService.getProjectListByUnitIds(bmProject);
    }

    @ApiOperation(value = "往来单位id和标段工程id获取协议信息")
    @PostMapping("getAgreementInfoById")
    public AjaxResult getAgreementInfoById(@RequestBody SelectDto dto) {
        return clzSltAgreementInfoService.getAgreementInfoById(dto);
    }

    @ApiOperation(value = "根据协议获取结算清单")
    @PostMapping("/getSltInfo")
    public AjaxResult getSltInfo(@RequestBody List<MaterialSltAgreementInfo> list) {
        List<String> unitNames = new ArrayList<>();
        List<String> projectNames = new ArrayList<>();
        List<MaterialSltInfoVo> dataList = new ArrayList<>();
        MaterialSltInfoVo bean = new MaterialSltInfoVo();
        Long agreementId = null;
        for (MaterialSltAgreementInfo info : list) {
            unitNames.add(info.getUnitName());
            projectNames.add(info.getProjectName());
            MaterialSltInfoVo vo = clzSltAgreementInfoService.getSltInfo(info);
            dataList.add(vo);
            agreementId = info.getAgreementId();
        }
        bean = clzSltAgreementInfoService.mergerData(bean, dataList,unitNames,projectNames);
        // 根据协议id获取申请时间
        TmTask tmTask = taskMapper.selectTaskByIdByCl(agreementId);
        if (tmTask != null) {
            bean.setApplyTime(tmTask.getCreateTime());
        }
        return AjaxResult.success(bean);
    }


    /**
     * 提交结算清单
     */
    @ApiOperation(value = "提交结算清单")
    @PostMapping("/submitCosts")
    public AjaxResult submitCosts(@RequestBody MaterialSltInfoVo sltInfoVo) {
        try {
            return toAjax(clzSltAgreementInfoService.submitCosts(sltInfoVo));
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }
}
