package com.ozs.web.controller.plan;

import com.github.pagehelper.PageInfo;
import com.ozs.common.core.controller.BaseController;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.core.domain.model.LoginUser;
import com.ozs.common.utils.poi.ExcelUtil;
import com.ozs.framework.web.service.TokenService;
import com.ozs.plan.doman.PlanYears;
import com.ozs.plan.doman.vo.requestVo.PlanYearsStandardVo;
import com.ozs.plan.service.PlanYearsService;
import io.swagger.annotations.ApiOperation;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 年度计划信息控制层
 *
 * @author buzhanyi
 */
@RestController
@RequestMapping("/plan/planYears")
public class PlanYearsController extends BaseController {
    @Autowired
    private PlanYearsService planYearsService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation(value = "查询年度计划")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody PlanYearsStandardVo yearsStandardVo) {
        //List<PlanYears> list = planYearsService.selectPlanYearsList(yearsStandardVo);
        PageInfo<PlanYears> page = planYearsService.selectPlanYearsList(yearsStandardVo);
        return AjaxResult.success(page);
    }

    @ApiOperation(value = "审核单位查询年度计划")
    @PostMapping("/examineList")
    public AjaxResult examineList(@RequestBody PlanYearsStandardVo yearsStandardVo) {
        PageInfo<PlanYears> page = planYearsService.selectPlanYearsExamineList(yearsStandardVo);
        return AjaxResult.success(page);
    }

    @ApiOperation(value = "导出年度计划数据")
    @PostMapping("/exportPlan")
    public void exportPlan(HttpServletResponse response, @RequestBody PlanYearsStandardVo yearsStandardVo) throws Exception {
        List<PlanYears> list = planYearsService.selectPlanYearsListEXP(yearsStandardVo);
        ExcelUtil<PlanYears> util = new ExcelUtil<>(PlanYears.class);
        util.exportExcel(response, list, "年度计划数据");
    }

    @ApiOperation(value = "导出年度计划数据(审核单位)")
    @PostMapping("/exportPlanExamine")
    public void exportPlanExamine(HttpServletResponse response, @RequestBody PlanYearsStandardVo yearsStandardVo) throws Exception {
        List<PlanYears> list = planYearsService.selectPlanYearsExamineListEXP(yearsStandardVo);
        ExcelUtil<PlanYears> util = new ExcelUtil<>(PlanYears.class);
        util.exportExcel(response, list, "年度计划数据(审核单位)");
    }

    @ApiOperation(value = "创建年度计划")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PlanYearsStandardVo yearsStandardVo, HttpServletRequest request) {
        //获取采购单位-
        LoginUser loginUser = tokenService.getLoginUser(request);
        yearsStandardVo.setPurchaseDeptId(String.valueOf(loginUser.getDeptId()));
        yearsStandardVo.setCreated(String.valueOf(loginUser.getUserId()));
        return planYearsService.insertPlanYears(yearsStandardVo);
    }

    @ApiOperation(value = "修改年度计划")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody PlanYearsStandardVo yearsStandardVo, HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        yearsStandardVo.setUpdated(String.valueOf(loginUser.getUserId()));
        return planYearsService.update(yearsStandardVo);
    }

    @ApiOperation(value = "提交年度计划")
    @PostMapping("/commit")
    public AjaxResult commit(@RequestBody PlanYearsStandardVo yearsStandardVo) {
        return planYearsService.commit(yearsStandardVo);
    }

    @ApiOperation(value = "删除年度计划")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody PlanYearsStandardVo yearsStandardVo) {
        return planYearsService.deletePlanYearsById(yearsStandardVo.getPlanYearId());
    }

    @ApiOperation(value = "根据id获取年度计划信息")
    @PostMapping("/view")
    public AjaxResult view(@RequestBody PlanYearsStandardVo yearsStandardVo) {
        return planYearsService.view(yearsStandardVo);
    }

    @ApiOperation(value = "审核年度计划通过")
    @PostMapping("/reviewTo")
    public AjaxResult reviewTo(@RequestBody PlanYearsStandardVo yearsStandardVo, HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        yearsStandardVo.setUpdated(String.valueOf(loginUser.getUserId()));
        return planYearsService.reviewTo(yearsStandardVo);
    }

    @ApiOperation(value = "审核年度计划退回")
    @PostMapping("/reviewReturn")
    public AjaxResult reviewReturn(@RequestBody PlanYearsStandardVo yearsStandardVo, HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        yearsStandardVo.setUpdated(String.valueOf(loginUser.getUserId()));
        return planYearsService.reviewReturn(yearsStandardVo);
    }

    @ApiOperation(value = "申请修改年度计划")
    @PostMapping("/appUpdate")
    public AjaxResult appUpdate(@RequestBody PlanYearsStandardVo yearsStandardVo) {
        return planYearsService.appUpdate(yearsStandardVo);
    }

    @ApiOperation(value = "发函催告")
    @PostMapping("/sendLetter")
    public AjaxResult sendLetter(@RequestBody PlanYearsStandardVo yearsStandardVo) {
        return planYearsService.sendLetter(yearsStandardVo);
    }

    @ApiOperation(value = "上传计划关联文件后保存文件信息")
    @PostMapping("/upLoadPlanFile")
    public AjaxResult upLoadPlanFile(@RequestBody PlanYearsStandardVo yearsStandardVo) {
        return planYearsService.upLoadPlanFile(yearsStandardVo);
    }
    //
    //@ApiOperation(value = "下载计划关联文件")
    //@PostMapping("/downLoadPlanFile")
    //public AjaxResult downLoadPlanFile(@RequestBody PlanYearsStandardVo yearsStandardVo) {
    //    return planYearsService.downLoadPlanFile(yearsStandardVo);
    //}

    @ApiOperation(value = "导入年度计划数据")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<PlanYearsStandardVo> util = new ExcelUtil<>(PlanYearsStandardVo.class);
        List<PlanYearsStandardVo> planYears = util.importExcel(file.getInputStream());
        //获取采购单位-
        LoginUser loginUser = getLoginUser();
        String message = planYearsService.importPlanYears(planYears, updateSupport, loginUser);
        return success(message);
    }

    @ApiOperation(value = "系统首页计划管理")
    @GetMapping("/planYearsTotal")
    public AjaxResult planYearsTotal(){
     return    success(planYearsService.count());
    }
}
