package net.ferrum.web.controller.business;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import net.ferrum.business.domain.QsBatch;
import net.ferrum.business.domain.QsProjects;
import net.ferrum.business.domain.QsSpecAttributeTypes;
import net.ferrum.business.domain.QsSpecAttributeValues;
import net.ferrum.business.domain.bo.QsProjectAttributesBo;
import net.ferrum.business.domain.bo.QsSpecAttributeValuesBo;
import net.ferrum.business.domain.bo.QsUserSupplierBo;
import net.ferrum.business.domain.vo.QsProjectAttributesVo;
import net.ferrum.business.service.IQsBatchService;
import net.ferrum.business.service.IQsProjectAttributesService;
import net.ferrum.business.service.IQsSpecAttributeValuesService;
import net.ferrum.common.utils.BeanCopyUtils;
import org.apache.ibatis.executor.BaseExecutor;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import net.ferrum.common.annotation.RepeatSubmit;
import net.ferrum.common.annotation.Log;
import net.ferrum.common.core.controller.BaseController;
import net.ferrum.common.core.domain.PageQuery;
import net.ferrum.common.core.domain.R;
import net.ferrum.common.core.validate.AddGroup;
import net.ferrum.common.core.validate.EditGroup;
import net.ferrum.common.core.validate.QueryGroup;
import net.ferrum.common.enums.BusinessType;
import net.ferrum.common.utils.poi.ExcelUtil;
import net.ferrum.business.domain.vo.QsProjectsVo;
import net.ferrum.business.domain.bo.QsProjectsBo;
import net.ferrum.business.service.IQsProjectsService;
import net.ferrum.common.core.page.TableDataInfo;

/**
 * 项目管理
 *
 * @author ruoyi
 * @date 2025-07-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/projects")
public class QsProjectsController extends BaseController {

    private final IQsProjectsService iQsProjectsService;
    private final IQsBatchService iQsBatchService;
    private final IQsSpecAttributeValuesService iQsSpecAttributeValuesService;
    private final IQsProjectAttributesService iQsProjectAttributesService;

    /**
     * 查询项目管理列表
     */
    @SaCheckPermission("business:projects:list")
    @GetMapping("/list")
    public TableDataInfo<QsProjects> list(QsProjects bo, PageQuery pageQuery) {
        return iQsProjectsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出项目管理列表
     */
    @SaCheckPermission("business:projects:export")
    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(QsProjectsBo bo, HttpServletResponse response) {
        List<QsProjectsVo> list = iQsProjectsService.queryList(bo);
        ExcelUtil.exportExcel(list, "项目管理", QsProjectsVo.class, response);
    }

    /**
     * 获取项目管理详细信息
     *
     * @param projectId 主键
     */
    @SaCheckPermission("business:projects:query")
    @GetMapping("/{projectId}")
    public R<QsProjects> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long projectId) {
        //return R.ok(iQsProjectsService.queryById(projectId));
        return R.ok(iQsProjectsService.selectById(projectId));
    }

    /**
     * 新增项目管理
     */
    @SaCheckPermission("business:projects:add")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()

    public R<Void> add(@Validated(AddGroup.class) @RequestBody QsProjectsBo bo) {
        return toAjax(iQsProjectsService.insertByBo(bo));
    }

    /**
     * 修改项目管理
     */
    @SaCheckPermission("business:projects:edit")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody QsProjectsBo bo) {
        return toAjax(iQsProjectsService.updateByBo(bo));

    }

    /**
     * 删除项目管理
     *
     * @param projectIds 主键串
     */
    @SaCheckPermission("business:projects:remove")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] projectIds) {

        String sRet = iQsProjectsService.deleteWithValidByIds(Arrays.asList(projectIds), true);
        if (sRet.length()==0){
            return R.ok();
         }else{
            return R.fail(sRet);
        }

    }

    /**
     * 获取批次列表
     */
    @SaCheckPermission("business:projects:list")
    @GetMapping("/batchList")
    public R< List<QsBatch>> selectBatchList(QsBatch type) {
        return R.ok(iQsBatchService.selectBatchList(type));
    }
}
