package com.ozs.web.controller.pm;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ozs.common.annotation.Log;
import com.ozs.common.constant.ModularConstans;
import com.ozs.common.core.controller.BaseController;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.enums.BusinessType;
import com.ozs.common.enums.PmProjectStatus;
import com.ozs.common.enums.SysFileRefEnum;
import com.ozs.common.utils.StringUtils;
import com.ozs.pm.doman.PmDemand;
import com.ozs.pm.doman.PmProjectConstruction;
import com.ozs.pm.doman.vo.requestVo.PmDemandReqVo;
import com.ozs.pm.doman.vo.requestVo.PmProjectConstructionReqVo;
import com.ozs.pm.doman.vo.responseVo.PmDemandResVo;
import com.ozs.pm.service.IPmDemandService;
import com.ozs.pm.service.PmProjectConstructionService;
import com.ozs.system.domain.SysFileRef;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import java.util.Date;
import java.util.List;

/**
 * 项目建设Controller
 *
 * @author ruoyi
 * @date 2023-01-16
 */
@Api(tags = "项目建设")
@RestController
@RequestMapping("/pm/projectConstruction")
public class PmProjectConstructionController extends BaseController {
    @Autowired
    private IPmDemandService pmDemandService;

    /**
     * 项目建设查询列表
     */
    @ApiOperation(value = "项目建设查询列表", notes = "参数非必传")
    @PostMapping("/list")
    @PreAuthorize("@ss.hasPermi('pm:projectConstruction:list')")
    @Log(title = ModularConstans.projectConstruction, businessType = BusinessType.QUERY)
    public AjaxResult list(@RequestBody PmDemandReqVo pmDemandReqVo) {
        IPage<PmDemandResVo> page  = pmDemandService.selectPmDemandList(pmDemandReqVo,5);
        return success(page);
    }

    @ApiOperation(value = "建设文档填制",notes = "采购需求ID和上传附件必传")
    @PostMapping("/insertProjectConstruction")
    @PreAuthorize("@ss.hasPermi('pm:projectConstruction:insertProjectConstruction')")
    @Log(title = ModularConstans.projectConstruction, businessType = BusinessType.INSERT)
    public AjaxResult insertProjectConstruction(@NotEmpty(message = "数据为空") @RequestBody PmProjectConstructionReqVo pmProjectConstructionReqVo) {
        try {
            pmProjectConstructionReqVo.setCreateBy(getUserId().toString());
            pmProjectConstructionReqVo.setCreateTime(new Date());
            pmProjectConstructionReqVo.setUpdateBy(getUserId().toString());
            pmProjectConstructionReqVo.setUpdateTime(pmProjectConstructionReqVo.getCreateTime());
            return toAjax(pmDemandService.insertProjectConstruction(pmProjectConstructionReqVo));
        } catch (Exception e){
            return error(e.getMessage());
        }
    }

    /**
     * 查看详情
     */
    @ApiOperation(value = "查看详情", notes = "必传demandId和详情类型(1项目计划，2需求建档，3任务下达,4中标信息，5合同信息，6建设情况),其他字段不传")
    @PostMapping("/view")
    @PreAuthorize("@ss.hasPermi('pm:projectConstruction:view')")
    @Log(title = ModularConstans.projectConstruction, businessType = BusinessType.QUERY)
    public AjaxResult view(@RequestBody PmDemandReqVo pmDemandReqVo) {
        if(pmDemandReqVo.getDemandId() == null){
            return AjaxResult.error("demandId不能为空");
        }
        if(StringUtils.isEmpty(pmDemandReqVo.getDetailType())){
            return AjaxResult.error("详情的类型不能为空");
        }
        return success(pmDemandService.selectPmDemandByDemandId(pmDemandReqVo.getDemandId(),pmDemandReqVo.getDetailType()));
    }
}
