package com.yanzhu.jh.project.controller;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.yanzhu.jh.project.domain.vo.MajorPlanExport;
import com.yanzhu.jh.project.domain.vo.UrbanPlanExport;
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.SurProjectPlan;
import com.yanzhu.jh.project.service.ISurProjectPlanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 项目跟进计划Controller
 * 
 * @author ruoyi
 * @date 2023-09-07
 */
@RestController
@RequestMapping("/project/projectPlan")
public class SurProjectPlanController extends BaseController
{
    @Autowired
    private ISurProjectPlanService surProjectPlanService;

    /**
     * 查询项目跟进计划列表
     */
    @PreAuthorize("@ss.hasPermi('project:projectPlan:list')")
    @GetMapping("/list")
    public TableDataInfo list(SurProjectPlan surProjectPlan)
    {
        startPage();
        List<SurProjectPlan> list = surProjectPlanService.selectSurProjectPlanList(surProjectPlan);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('project:projectPlan:list')")
    @GetMapping("/listAll/{type}")
    public AjaxResult listAll(@PathVariable("type") Long type){
        SurProjectPlan where=new SurProjectPlan();
        where.setPlanType(type);
        where.setIsDel(0l);
        List<SurProjectPlan> list = surProjectPlanService.selectSurProjectPlanList(where);
        return AjaxResult.success(list);
    }
    /**
     * 导出项目跟进计划列表
     */
    @PreAuthorize("@ss.hasPermi('project:projectPlan:export')")
    @Log(title = "项目跟进计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export/{type}")
    public void export(HttpServletResponse response,@PathVariable("type") Long type)
    {
        SurProjectPlan where=new SurProjectPlan();
        where.setPlanType(type);
        List<SurProjectPlan> list = surProjectPlanService.selectSurProjectPlanList(where);
        if(list.size()==0){
            list.add(new SurProjectPlan());
        }
        if(type==0){
            List<UrbanPlanExport> list1=new ArrayList<>();
            for(SurProjectPlan it :list){
                list1.add(UrbanPlanExport.convert(it));
            }
            ExcelUtil<UrbanPlanExport> util=new ExcelUtil<>(UrbanPlanExport.class);
            util.exportExcel(response, list1, "城建项目计划");
        }else{
            List<MajorPlanExport> list1=new ArrayList<>();
            for(SurProjectPlan it :list){
                list1.add(MajorPlanExport.convert(it));
            }
            ExcelUtil<MajorPlanExport> util=new ExcelUtil<>(MajorPlanExport.class);
            util.exportExcel(response, list1, "重点项目计划");
        }

    }

    /**
     * 获取项目跟进计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:projectPlan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(surProjectPlanService.selectSurProjectPlanById(id));
    }

    /**
     * 新增项目跟进计划
     */
    @PreAuthorize("@ss.hasPermi('project:projectPlan:add')")
    @Log(title = "项目跟进计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SurProjectPlan surProjectPlan)
    {
        return toAjax(surProjectPlanService.insertSurProjectPlan(surProjectPlan));
    }

    /**
     * 修改项目跟进计划
     */
    @PreAuthorize("@ss.hasPermi('project:projectPlan:edit')")
    @Log(title = "项目跟进计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SurProjectPlan surProjectPlan)
    {
        return toAjax(surProjectPlanService.updateSurProjectPlan(surProjectPlan));
    }

    /**
     * 删除项目跟进计划
     */
    @PreAuthorize("@ss.hasPermi('project:projectPlan:remove')")
    @Log(title = "项目跟进计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(surProjectPlanService.deleteSurProjectPlanByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('project:projectPlan:import')")
    @PostMapping("/importUrbanPlan")
    public AjaxResult importUrbanPlan(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<UrbanPlanExport> util=new ExcelUtil<UrbanPlanExport>(UrbanPlanExport.class);
        InputStream is=file.getInputStream();
        List<UrbanPlanExport> list = util.importExcel(is);
        is.close();
        int n=surProjectPlanService.importUrbanPlan(list);
        return success("成功导入"+n+"条数据!");
    }

    @PreAuthorize("@ss.hasPermi('project:projectPlan:import')")
    @PostMapping("/importMajorPlan")
    public AjaxResult importMajorPlan(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<MajorPlanExport> util=new ExcelUtil<MajorPlanExport>(MajorPlanExport.class);
        InputStream is=file.getInputStream();
        List<MajorPlanExport> list = util.importExcel(is);
        is.close();
        int n=surProjectPlanService.importMajorPlan(list);
        return success("成功导入"+n+"条数据!");
    }

}
