package com.gyee.runeconomy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gyee.common.config.R;
import com.gyee.common.model.StringUtils;
import com.gyee.runeconomy.dto.ResultMsg;
import com.gyee.runeconomy.model.auto.ProBasicProjectPlan;
import com.gyee.runeconomy.service.auto.IProBasicProjectPlanService;
import com.gyee.runeconomy.util.ExcelUtils;
import com.gyee.runeconomy.util.SnowflakeGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("//pro-basic-project-plan")
@Api(value = "工程期次计划电量配置", tags = "工程期次计划电量配置")
public class ProBasicProjectPlanController {
    @Resource
    private IProBasicProjectPlanService proBasicProjectPlanService;

    /**
     * 查询
     *
     * @param id
     * @param projectId
     * @param windpowerstationId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation(value = "工程计划电量-列表", notes = "工程计划电量-列表")
    public R findList(@RequestParam(value = "id", required = false) String id,
                      @RequestParam(value = "projectId", required = false) String projectId,
                      @RequestParam(value = "windpowerstationId", required = false) String windpowerstationId,
                      @RequestParam(value = "year", required = false) String year,
                      @RequestParam(value = "month", required = false) String month,
                      @RequestParam(value = "modificationTime", required = false) String modificationTime,
                      @RequestParam(value = "pageNum", required = true) String pageNum,
                      @RequestParam(value = "pageSize", required = true) String pageSize) {
        IPage<ProBasicProjectPlan> list = proBasicProjectPlanService.List(id, projectId, windpowerstationId, year, month, modificationTime, pageNum, pageSize);
        if (null != list) {
            return R.ok().data(list);
        } else {
            return R.error().data("查询失败！");
        }
    }

    /**
     * 新增
     *
     * @param proBasicProjectPlan
     * @return
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "工程计划电量-新增or修改", notes = "工程计划电量-新增or修改")
    public R addAll(@RequestBody ProBasicProjectPlan proBasicProjectPlan) {

        boolean b = proBasicProjectPlanService.saveOrUpdate(proBasicProjectPlan);
        if (b) {
            return R.ok().data(b);
        } else {
            return R.error().data("保存失败！");
        }
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/remove-ProjectPlan/{ids}")
    @ApiOperation(value = "工程计划电量-删除", notes = "工程计划电量-删除")
    public R deleteAll(@PathVariable("ids") String ids) {
        String[] strings = ids.split(",");

        boolean b = proBasicProjectPlanService.removeByIds(Arrays.asList(strings));

        if (b) {
            return R.ok().data(b);
        } else {
            return R.error().data("删除失败！");
        }
    }

    /**
     * 获得导入模版
     * @param response
     * @throws IOException
     */
    //    @GetMapping("/get-import-template")
    //    @ApiOperation(value = "获得导入模板")
    //    public void importTemplate(HttpServletResponse response) throws IOException {
    //        // 手动创建导出 demo
    //        Date current = com.gyee.common.util.DateUtils.getCurrentDate();
    //        Calendar calendar = Calendar.getInstance();
    //        calendar.setTime(current);
    //        calendar.add(Calendar.DAY_OF_MONTH, -1);
    //        Date previousDay = calendar.getTime();
    //
    //        List<ProBasicProjectPlan> list = Arrays.asList(
    //                ProBasicProjectPlan.builder().id("").generatingCapacity(1.0).maintenancePowe("2.0").month("3").year("2023").outageHours(4.0).windpowerstationId("SXJ_KGDL_GJY_FDC_STA").projectId("SXJ_KGDL_GJYF03_EG").modificationTime(DateUtils.truncate(previousDay)).build()
    //
    //        );
    //
    //        // 输出
    //        ExcelUtils.write(response, "导入模板.xls", "导入模板", ProBasicProjectPlan.class, list);
    //    }

    /**
     * 导入
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/import")
    @ApiOperation(value = "导入", notes = "导入")
    public com.gyee.runeconomy.dto.R importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        List<ProBasicProjectPlan> list = ExcelUtils.read(file, ProBasicProjectPlan.class);

        if (StringUtils.notEmp(list) && !list.isEmpty()) {
            for (ProBasicProjectPlan vo : list) {
                vo.setId(SnowflakeGenerator.generateId());
                //                vo.setOperationTime(new Date());

                //                List<ProBasicProjectPlan > ls=ProBasicProjectPlan.getByDateAndName(vo.getName(),vo.getRecordDate());
                //                if(ls.isEmpty())
                //                {
                proBasicProjectPlanService.save(vo);
                //                }

            }
        }
        if (StringUtils.isNotNull(list)) {
            return com.gyee.runeconomy.dto.R.data(ResultMsg.ok(list));
        } else {
            return com.gyee.runeconomy.dto.R.error(ResultMsg.error());
        }
    }

}
