package com.gyee.runeconomy.controller.update;

import com.gyee.common.model.StringUtils;
import com.gyee.runeconomy.dto.R;
import com.gyee.runeconomy.dto.ResultMsg;
import com.gyee.runeconomy.model.auto.ProEconTargetdataFrist;
import com.gyee.runeconomy.service.update.TargetdataFristService;
import com.gyee.runeconomy.util.DateUtils;
import com.gyee.runeconomy.util.ExcelUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("//enterFrist")
public class TargetdataFristController {
    @Resource
    private TargetdataFristService targetdataFristService;


    @PostMapping(value = "/save")
    @ApiOperation(value = "增", notes = "增")
    public R save(@RequestBody ProEconTargetdataFrist data) {
        data.setOperationTime(new Date());
        boolean b = targetdataFristService.save(data);
        if (b) {
            return R.data(ResultMsg.ok(b));
        } else {
            return R.error(ResultMsg.error());
        }
    }


    @PutMapping(value = "/update")
    @ApiOperation(value = "改", notes = "改")
    public R update(@RequestBody ProEconTargetdataFrist data) {
        data.setOperationTime(new Date());
        boolean b = targetdataFristService.update(data);
        if (b) {
            return R.data(ResultMsg.ok(b));
        } else {
            return R.error(ResultMsg.error());
        }
    }

    @DeleteMapping(value = "/delete/{id}")
    @ApiOperation(value = "删", notes = "删")
    public R delete(@PathVariable String id) {
        boolean b = targetdataFristService.delete(id);
        if (b) {
            return R.data(ResultMsg.ok(b));
        } else {
            return R.error(ResultMsg.error());
        }
    }

    @GetMapping(value = "/get")
    @ApiOperation(value = "查", notes = "查")
    public R get(
            @RequestParam(value = "date", required = true) String date) {
        List<ProEconTargetdataFrist> resultList = targetdataFristService.get(date);
        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }

    @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<ProEconTargetdataFrist> list = Arrays.asList(
                ProEconTargetdataFrist.builder().id("").name("风电").type("FD").rfdl(1.0).yfdl(2.0).nfdl(3.0).recordDate(DateUtils.truncate(previousDay)).build(),

                ProEconTargetdataFrist.builder().id("").name("光伏").type("GF").rfdl(1.0).yfdl(2.0).nfdl(3.0).recordDate(DateUtils.truncate(previousDay)).build()

        );

        // 输出
        ExcelUtils.write(response, "导入模板.xls", "导入模板", ProEconTargetdataFrist.class, list);
    }

    @PostMapping("/import")
    @ApiOperation(value = "导入", notes = "导入")
    public R importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        List<ProEconTargetdataFrist> list = ExcelUtils.read(file, ProEconTargetdataFrist.class);

        if (StringUtils.notEmp(list) && !list.isEmpty()) {
            for (ProEconTargetdataFrist vo : list) {
                vo.setId(StringUtils.getUUID());
                vo.setOperationTime(new Date());

                List<ProEconTargetdataFrist> ls = targetdataFristService.getByDateAndName(vo.getName(), vo.getRecordDate());
                if (ls.isEmpty()) {
                    targetdataFristService.save(vo);
                }

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


}
