package com.yanzhu.jh.project.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.yanzhu.jh.project.domain.SurProjectBuildDesignData;
import com.yanzhu.jh.project.service.ISurProjectBuildDesignDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设计管理Controller
 * 
 * @author ruoyi
 * @date 2024-03-16
 */
@RestController
@RequestMapping("/project/projectDesign")
public class SurProjectBuildDesignDataController extends BaseController
{
    @Autowired
    private ISurProjectBuildDesignDataService surProjectBuildDesignDataService;

    /**
     * 查询设计管理列表
     */
    @PreAuthorize("@ss.hasPermi('project:projectDesign:list')")
    @GetMapping("/list")
    public TableDataInfo list(SurProjectBuildDesignData surProjectBuildDesignData)
    {
        startPage();
        List<SurProjectBuildDesignData> list = surProjectBuildDesignDataService.selectSurProjectBuildDesignDataList(surProjectBuildDesignData);
        return getDataTable(list);
    }

    /**
     * 导出设计管理列表
     */
    @PreAuthorize("@ss.hasPermi('project:projectDesign:export')")
    @Log(title = "设计管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SurProjectBuildDesignData surProjectBuildDesignData)
    {
        List<SurProjectBuildDesignData> list = surProjectBuildDesignDataService.selectSurProjectBuildDesignDataList(surProjectBuildDesignData);
        ExcelUtil<SurProjectBuildDesignData> util = new ExcelUtil<SurProjectBuildDesignData>(SurProjectBuildDesignData.class);
        util.exportExcel(response, list, "设计管理数据");
    }

    /**
     * 获取设计管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:projectDesign:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(surProjectBuildDesignDataService.selectSurProjectBuildDesignDataById(id));
    }

    /**
     * 新增设计管理
     */
    @PreAuthorize("@ss.hasPermi('project:projectDesign:add')")
    @Log(title = "设计管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SurProjectBuildDesignData surProjectBuildDesignData)
    {
        return toAjax(surProjectBuildDesignDataService.insertSurProjectBuildDesignData(surProjectBuildDesignData));
    }

    /**
     * 修改设计管理
     */
    @PreAuthorize("@ss.hasPermi('project:projectDesign:edit')")
    @Log(title = "设计管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SurProjectBuildDesignData surProjectBuildDesignData)
    {
        return toAjax(surProjectBuildDesignDataService.updateSurProjectBuildDesignData(surProjectBuildDesignData));
    }


    /**
     * 修改设计管理
     */
    @PreAuthorize("@ss.hasPermi('project:projectDesign:edit')")
    @Log(title = "设计管理", businessType = BusinessType.UPDATE)
    @PostMapping("updateList")
    public AjaxResult updateList(@RequestBody List<SurProjectBuildDesignData> list)
    {
        return  AjaxResult.success (surProjectBuildDesignDataService.updateList(list));
    }

    /**
     * 删除设计管理
     */
    @PreAuthorize("@ss.hasPermi('project:projectDesign:remove')")
    @Log(title = "设计管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(surProjectBuildDesignDataService.deleteSurProjectBuildDesignDataByIds(ids));
    }

    /**
     * 查询设计管理列表 按项目查询
     */
    @PreAuthorize("@ss.hasPermi('project:projectDesign:list')")
    @GetMapping("/listByProject")
    public AjaxResult listByProject(SurProjectBuildDesignData where)
    {
        //按项目ID查找设计管理数据，日期为当前周内，如果没有就增加一组数据，增加的数据先找后一条数据进行克隆，如果没有就增加空白数据
        where.setStartDate(DateUtil.beginOfWeek(DateTime.now()));
        where.setEndDate(DateUtil.endOfWeek(DateTime.now()));
        List<SurProjectBuildDesignData> list=surProjectBuildDesignDataService.selectWeekData(where);
        if(list.size()==0){
            surProjectBuildDesignDataService.addWeekData(where);
            list=surProjectBuildDesignDataService.selectWeekData(where);
        }
        return AjaxResult.success(list);
    }
}
