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.PlanQuarter;
import com.ozs.plan.doman.vo.requestVo.PlanQuarterStandardVo;
import com.ozs.plan.service.PlanQuarterService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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/quarter")
public class PlanQuarterController extends BaseController {
    @Autowired
    private PlanQuarterService quarterService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation(value = "查询季度计划")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody PlanQuarterStandardVo quarterStandardVo) {
        PageInfo<PlanQuarter> page = quarterService.selectPlanQuarterList(quarterStandardVo);
        return AjaxResult.success(page);
    }

    @ApiOperation(value = "审核单位查询季度计划")
    @PostMapping("/examineList")
    public AjaxResult examineList(@RequestBody PlanQuarterStandardVo quarterStandardVo) {
        PageInfo<PlanQuarter> page = quarterService.selectPlanQuarterExamineList(quarterStandardVo);
        return AjaxResult.success(page);
    }

    @ApiOperation(value = "导出季度计划数据")
    @PostMapping("/exportPlan")
    public void exportPlan(HttpServletResponse response, @RequestBody PlanQuarterStandardVo quarterStandardVo) throws Exception {
        List<PlanQuarter> list = quarterService.selectPlanQuarterListEXP(quarterStandardVo);
        ExcelUtil<PlanQuarter> util = new ExcelUtil<>(PlanQuarter.class);
        util.exportExcel(response, list, "季度计划数据");
    }

    @ApiOperation(value = "导出季度计划数据(审核单位)")
    @PostMapping("/exportPlanExamine")
    public void exportPlanExamine(HttpServletResponse response, @RequestBody PlanQuarterStandardVo quarterStandardVo) throws Exception {
        List<PlanQuarter> list = quarterService.selectPlanQuarterExamineListEXP(quarterStandardVo);
        ExcelUtil<PlanQuarter> util = new ExcelUtil<>(PlanQuarter.class);
        util.exportExcel(response, list, "季度计划数据(审核单位)");
    }

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

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

    @ApiOperation(value = "提交季度计划")
    @PostMapping("/commit")
    public AjaxResult commit(@RequestBody PlanQuarterStandardVo quarterStandardVo) {
        return quarterService.commit(quarterStandardVo);
    }

    @ApiOperation(value = "删除季度计划")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody PlanQuarterStandardVo quarterStandardVo) {
        return quarterService.deletePlanQuarterById(quarterStandardVo.getPlanPracticalId());
    }

    @ApiOperation(value = "根据id获取季度计划信息")
    @PostMapping("/view")
    public AjaxResult view(@RequestBody PlanQuarterStandardVo quarterStandardVo) {
        return quarterService.view(quarterStandardVo);
    }

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

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

    @ApiOperation(value = "申请修改季度计划")
    @PostMapping("/appUpdate")
    public AjaxResult appUpdate(@RequestBody PlanQuarterStandardVo quarterStandardVo) {
        return quarterService.appUpdate(quarterStandardVo);
    }

    @ApiOperation(value = "发函催告")
    @PostMapping("/sendLetter")
    public AjaxResult sendLetter(@RequestBody PlanQuarterStandardVo quarterStandardVo) {
        return quarterService.sendLetter(quarterStandardVo);
    }

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

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


}
