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.SRaHandworkEmissionsGas;
import cn.cecep.talroad.service.analysis.ISRaHandworkEmissionsGasService;
import cn.cecep.talroad.service.analysis.ISRaHandworkEmissionsWaterService;
import cn.cecep.talroad.vo.analysis.SRaHandworkEmissionsGasVo;
import cn.cecep.talroad.vo.query.analysis.SRaHandworkEmissionsGasQuery;
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.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
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("/gas")
public class SRaHandworkEmissionsGasController extends BasesController {
    @Autowired
    private ISRaHandworkEmissionsGasService sRaHandworkEmissionsGasService;
    @Autowired
    private ISRaHandworkEmissionsWaterService sRaHandworkEmissionsWaterService;
    /**
     * 查询手工监测排放监控-气污染要素列表
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/list")
    public AjaxResults<IPage<SRaHandworkEmissionsGas>> list(SRaHandworkEmissionsGasQuery sRaHandworkEmissionsGas) {
//        Calendar c = Calendar.getInstance();
//        Date date = new Date();
//        c.setTime(date);
//        c.add(Calendar.DATE, - 1);
//        Date toDate = c.getTime();
//        if(StringUtils.isEmpty(sRaHandworkEmissionsGas.getMonitorTimeStart()) || StringUtils.isEmpty(sRaHandworkEmissionsGas.getMonitorTimeEnd())){
//            sRaHandworkEmissionsGas.setMonitorTimeStart(DateUtil.format(toDate, DatePattern.NORM_DATETIME_PATTERN));
//            sRaHandworkEmissionsGas.setMonitorTimeEnd(DateUtil.format(date, DatePattern.NORM_DATETIME_PATTERN));
//        }
//        if(StringUtils.isEmpty(sRaHandworkEmissionsGas.getCheckCycleStart()) || StringUtils.isEmpty(sRaHandworkEmissionsGas.getCheckCycleEnd())){
//            sRaHandworkEmissionsGas.setCheckCycleStart(DateUtil.format(toDate, DatePattern.NORM_DATETIME_PATTERN));
//            sRaHandworkEmissionsGas.setCheckCycleEnd(DateUtil.format(date, DatePattern.NORM_DATETIME_PATTERN));
//        }
        Page<SRaHandworkEmissionsGas> page = new Page<>(sRaHandworkEmissionsGas.getPageNum(), sRaHandworkEmissionsGas.getPageSize());
        IPage<SRaHandworkEmissionsGas> list = sRaHandworkEmissionsGasService.page(page, SRaHandworkEmissionsGasQuery.createLambdaQueryWrapper(sRaHandworkEmissionsGas));
        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 SRaHandworkEmissionsGasQuery sRaHandworkEmissionsGas) {
        List<SRaHandworkEmissionsGas> list = sRaHandworkEmissionsGasService.selectSRaHandworkEmissionsGasList(sRaHandworkEmissionsGas);
        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<SRaHandworkEmissionsGas> util = new ExcelUtil<SRaHandworkEmissionsGas>(SRaHandworkEmissionsGas.class);
        util.exportExcel(response, list, "手工监测排放监控-气污染要素数据");
    }

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

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

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

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

    /**
     * 监测点位名称options
     */
    @ApiOperation(value = "监测点位名称options")
    @GetMapping("/pullPointOptions")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pollutantType", value = "污染物类型 （废气有组织排口：1｜废水排口：2）", required = true, paramType = "query", dataType = "String"),
    })
    public AjaxResults<List<Map<String, String>>> pullPointOptions(@RequestParam(value = "pollutantType") String pollutantType) {
        if(pollutantType.equals("1")){
            return AjaxResults.success(sRaHandworkEmissionsGasService.pullPointOptions());
        }else {
            return AjaxResults.success(sRaHandworkEmissionsWaterService.pullPointOptions());
        }
    }

    /**
     * 污染物名称options
     */
    @ApiOperation(value = "污染物名称options")
    @GetMapping("/pullPollutantOptions")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pollutantType", value = "污染物类型 （废气有组织排口：1｜废水排口：2）", required = true, paramType = "query", dataType = "String"),
    })
    public AjaxResults<List<Map<String, String>>> pullPollutantOptions(@RequestParam(value = "pollutantType") String pollutantType) {
        if(pollutantType.equals("1")){
            return AjaxResults.success(sRaHandworkEmissionsGasService.pullPollutantOptions());
        }else {
            return AjaxResults.success(sRaHandworkEmissionsWaterService.pullPollutantOptions());
        }
    }
}
