package cn.cecep.talroad.controller.analysis;

import cn.cecep.talroad.common.core.exception.CommonException;
import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.common.core.web.controller.BasesController;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.log.annotation.Log;
import cn.cecep.talroad.common.log.enums.BusinessType;
import cn.cecep.talroad.common.security.annotation.RequiresPermissions;
import cn.cecep.talroad.domain.analysis.SRaHandworkEmissionsWater;
import cn.cecep.talroad.service.analysis.ISRaHandworkEmissionsWaterService;
import cn.cecep.talroad.vo.analysis.SRaHandworkEmissionsWaterVo;
import cn.cecep.talroad.vo.query.analysis.SRaHandworkEmissionsWaterQuery;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.security.InvalidParameterException;
import java.util.*;


/**
 * * 开发公司：中节能天融科技有限公司
 * 手工监测排放监控-水污染要素Controller 控制层
 *
 * @author szhpt
 * @date 2023-03-24
 */
@Api(tags = "A手工监测排放监控-水污染要素")
@RestController
@RequestMapping("/water")
public class SRaHandworkEmissionsWaterController extends BasesController {
    @Autowired
    private ISRaHandworkEmissionsWaterService sRaHandworkEmissionsWaterService;

    /**
     * 查询手工监测排放监控-水污染要素列表
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/list")
    public AjaxResults<IPage<SRaHandworkEmissionsWater>> list(SRaHandworkEmissionsWaterQuery sRaHandworkEmissionsWater) {
//        Calendar c = Calendar.getInstance();
//        Date date = new Date();
//        c.setTime(date);
//        c.add(Calendar.DATE, - 1);
//        Date toDate = c.getTime();
//        if(StringUtils.isEmpty(sRaHandworkEmissionsWater.getMonitorTimeStart()) || StringUtils.isEmpty(sRaHandworkEmissionsWater.getMonitorTimeEnd())){
//            sRaHandworkEmissionsWater.setMonitorTimeStart(DateUtil.format(toDate, DatePattern.NORM_DATETIME_PATTERN));
//            sRaHandworkEmissionsWater.setMonitorTimeEnd(DateUtil.format(date, DatePattern.NORM_DATETIME_PATTERN));
//        }
//        if(StringUtils.isEmpty(sRaHandworkEmissionsWater.getCheckCycleStart()) || StringUtils.isEmpty(sRaHandworkEmissionsWater.getCheckCycleEnd())){
//            sRaHandworkEmissionsWater.setCheckCycleStart(DateUtil.format(toDate, DatePattern.NORM_DATETIME_PATTERN));
//            sRaHandworkEmissionsWater.setCheckCycleEnd(DateUtil.format(date, DatePattern.NORM_DATETIME_PATTERN));
//        }
        Page<SRaHandworkEmissionsWater> page = new Page<>(sRaHandworkEmissionsWater.getPageNum(), sRaHandworkEmissionsWater.getPageSize());
        IPage<SRaHandworkEmissionsWater> list = sRaHandworkEmissionsWaterService.page(page, SRaHandworkEmissionsWaterQuery.createLambdaQueryWrapper(sRaHandworkEmissionsWater));
        if(!CollectionUtils.isEmpty(list.getRecords())){
            list.getRecords().stream().forEach(item->{
                if(item.getCheckCycleStart() != null && item.getCheckCycleEnd() != null){
                    item.setCheckTime(DateUtil.format(item.getCheckCycleStart(), DatePattern.NORM_DATE_PATTERN) + "-" + DateUtil.format(item.getCheckCycleEnd(), DatePattern.NORM_DATE_PATTERN));
                }
            });
        }
        return AjaxResults.success(list);
    }


    /**
     * 导出手工监测排放监控-水污染要素列表
     */
    @ApiOperation(value = "导出")
    @Log(title = "手工监测排放监控-水污染要素", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody SRaHandworkEmissionsWaterQuery sRaHandworkEmissionsWater) {
        List<SRaHandworkEmissionsWater> list = sRaHandworkEmissionsWaterService.selectSRaHandworkEmissionsWaterList(sRaHandworkEmissionsWater);
        if(!CollectionUtils.isEmpty(list)){
            list.stream().forEach(item->{
                if(item.getCheckCycleStart() != null && item.getCheckCycleEnd() != null){
                    item.setCheckTime(DateUtil.format(item.getCheckCycleStart(), DatePattern.NORM_DATE_PATTERN) + "-" + DateUtil.format(item.getCheckCycleEnd(), DatePattern.NORM_DATE_PATTERN));
                }
            });
        }
        ExcelUtil<SRaHandworkEmissionsWater> util = new ExcelUtil<SRaHandworkEmissionsWater>(SRaHandworkEmissionsWater.class);
        util.exportExcel(response, list, "手工监测排放监控-水污染要素数据");
    }

    /**
     * 获取手工监测排放监控-水污染要素详细信息
     */
    @ApiOperation(value = "详情")
    @PostMapping(value = "/{id}")
    public AjaxResults<SRaHandworkEmissionsWater> getInfo(@PathVariable("id") String id) {
        return AjaxResults.success(sRaHandworkEmissionsWaterService.getById(id));
    }

    /**
     * 新增手工监测排放监控-水污染要素
     */
    @ApiOperation(value = "新增")
    @Log(title = "手工监测排放监控-水污染要素", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody SRaHandworkEmissionsWater sRaHandworkEmissionsWater) {
        return toAjax(sRaHandworkEmissionsWaterService.save(sRaHandworkEmissionsWater));
    }

    /**
     * 修改手工监测排放监控-水污染要素
     */
    @ApiOperation(value = "修改")
    @Log(title = "手工监测排放监控-水污染要素", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody SRaHandworkEmissionsWaterVo sRaHandworkEmissionsWaterVo) {
        if (ObjectUtil.isEmpty(sRaHandworkEmissionsWaterVo)) {
            throw new InvalidParameterException("参数为空");
        }
        if (ObjectUtil.isEmpty(sRaHandworkEmissionsWaterVo.getId())) {
            throw new InvalidParameterException("参数id为空");
        }
        SRaHandworkEmissionsWater sRaHandworkEmissionsWater = sRaHandworkEmissionsWaterService.getById(sRaHandworkEmissionsWaterVo.getId());
        if (ObjectUtil.isEmpty(sRaHandworkEmissionsWater)) {
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", sRaHandworkEmissionsWaterVo.getId()));
        }
        //赋值
        BeanUtil.copyProperties(sRaHandworkEmissionsWaterVo, sRaHandworkEmissionsWater);
        return toAjax(sRaHandworkEmissionsWaterService.saveOrUpdate(sRaHandworkEmissionsWater));
    }

    /**
     * 删除手工监测排放监控-水污染要素
     */
    @ApiOperation(value = "删除")
    @RequiresPermissions("talroad:water:remove")
    @Log(title = "手工监测排放监控-水污染要素", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResults<Boolean> remove(@PathVariable String[] ids) {
        return toAjax(sRaHandworkEmissionsWaterService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 监测点位名称options
     */
    @ApiOperation(value = "监测点位名称options")
    @GetMapping("/pullPointOptions")
    public AjaxResults<List<Map<String, String>>> pullPointOptions() {
        return AjaxResults.success(sRaHandworkEmissionsWaterService.pullPointOptions());
    }

    /**
     * 污染物名称options
     */
    @ApiOperation(value = "污染物名称options")
    @GetMapping("/pullPollutantOptions")
    public AjaxResults<List<Map<String, String>>> pullPollutantOptions() {
        return AjaxResults.success(sRaHandworkEmissionsWaterService.pullPollutantOptions());
    }
}
