/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.xrtygis.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.xrtygis.commons.Constant;
import com.tofly.xrtygis.custom.util.DateTimeUtil;
import com.tofly.xrtygis.entity.Customize;
import com.tofly.xrtygis.entity.IndicatorCorrection;
import com.tofly.xrtygis.entity.dto.CustomizeDto;
import com.tofly.xrtygis.service.CustomizeService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义指标修正记录表
 *
 * @author admin
 * @date Thu Apr 28 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/customize")
@Api(tags = "自定义指标修正记录表接口")
public class CustomizeController {

    private final CustomizeService customizeService;

    /**
     * 分页查询
     *
     * @param page      分页对象
     * @param customize 自定义指标修正记录表
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone getCustomizePage(Page page, @ModelAttribute Customize customize) {
        return ResultRespone.success(customizeService.page(page, new QueryWrapper<Customize>().lambda()
                .like(null != customize.getCreateUser(), Customize::getCreateUser, customize.getCreateUser())
                .like(StringUtils.isNotBlank(customize.getDmaName()), Customize::getDmaName, customize.getDmaName())
                .eq(null!=customize.getDmaId() && customize.getDmaId()!=0,Customize::getDmaId,customize.getDmaId())

                .ge(null != customize.getSelEndTime(), Customize::getReviseDate, customize.getSelEndTime())
                .le(null != customize.getSelEndTime(), Customize::getReviseDate, customize.getSelEndTime())));
    }


    /**
     * 通过id查询自定义指标修正记录表
     *
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("id") Long id) {
        return ResultRespone.success(customizeService.getById(id));
    }

    /**
     * 新增自定义指标修正记录表
     *
     * @param dto 修正系数对象
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "自定义修正")
    @ApiOperation(value = "自定义修正")
    @PostMapping("/customFix")
    public ResultRespone save(@RequestBody CustomizeDto dto) {
        // 判断所修正月份是否是包含当前月份 若包含当前月份则不允许修改
        String months = dto.getMonths();
        if(StringUtils.isNotBlank(months)){
            String dateTime = DateTimeUtil.getLocalDateTime(LocalDateTime.now(), DateTimeUtil.DAY_FORMAT);
            if (dto.getMonths().contains(dateTime)) {
                return ResultRespone.success("当前月份不允许修改");
            }
            return ResultRespone.success(customizeService.saveInfo(dto, ""));
        }else {
            return ResultRespone.failed("请传入统计月份格式YYYY-MM-DD");
        }

    }


    /**
     * 通过id批量删除自定义指标修正记录表
     *
     * @param ids id
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "通过id批量删除自定义指标修正记录表")
    @ApiOperation(value = "通过ID批量删除自定义指标修正记录表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    public ResultRespone removeById(String ids) {
        return ResultRespone.success(customizeService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
