package cn.cecep.talroad.controller.analysis;

import cn.cecep.talroad.common.core.domain.R;
import cn.cecep.talroad.common.core.exception.CommonException;
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.common.security.utils.SecurityUtils;
import cn.cecep.talroad.domain.analysis.SRaEmissionsStandard;
import cn.cecep.talroad.feign.BaseRegionService;
import cn.cecep.talroad.service.analysis.ISRaEmissionsStandardService;
import cn.cecep.talroad.vo.analysis.SRaEmissionsStandardVo;
import cn.cecep.talroad.vo.query.analysis.SRaEmissionsStandardQuery;
import cn.hutool.core.bean.BeanUtil;
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.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * * 开发公司：中节能天融科技有限公司
 * 企业排放水平分析Controller 控制层
 *
 * @author szhpt
 * @date 2023-03-24
 */
@Api(tags = "A企业排放水平分析")
@RestController
@RequestMapping("/standard")
public class SRaEmissionsStandardController extends BasesController {
    @Autowired
    private ISRaEmissionsStandardService sRaEmissionsStandardService;

    @Autowired
    @Qualifier("baseRegionService")
    private BaseRegionService baseRegionService;
    /**
     * 查询企业排放水平分析列表
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/list")
    public AjaxResults<IPage<SRaEmissionsStandard>> list(SRaEmissionsStandardQuery sRaEmissionsStandard) {
        String regionCode = SecurityUtils.getLoginUser().getSysUser().getRegionCode();

        List<String> regionCodes=new ArrayList<>();
        R<List<String>> r = baseRegionService.getRegionCodeListChild(regionCode);
        if(r.getCode()==200){
            regionCodes=r.getData();
        }
        sRaEmissionsStandard.setRegionCodes(regionCodes);
        Page<SRaEmissionsStandard> page = new Page<>(sRaEmissionsStandard.getPageNum(), sRaEmissionsStandard.getPageSize());
        IPage<SRaEmissionsStandard> list = sRaEmissionsStandardService.page(page, SRaEmissionsStandardQuery.createLambdaQueryWrapper(sRaEmissionsStandard));
        return AjaxResults.success(list);
    }


    /**
     * 导出企业排放水平分析列表
     */
    @ApiOperation(value = "导出")
    @Log(title = "企业排放水平分析", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody SRaEmissionsStandardQuery sRaEmissionsStandard) {
        String regionCode = SecurityUtils.getLoginUser().getSysUser().getRegionCode();

        List<String> regionCodes=new ArrayList<>();
        R<List<String>> r = baseRegionService.getRegionCodeListChild(regionCode);
        if(r.getCode()==200){
            regionCodes=r.getData();
        }
        sRaEmissionsStandard.setRegionCodes(regionCodes);
        List<SRaEmissionsStandard> list = sRaEmissionsStandardService.selectSRaEmissionsStandardList(sRaEmissionsStandard);
        ExcelUtil<SRaEmissionsStandard> util = new ExcelUtil<SRaEmissionsStandard>(SRaEmissionsStandard.class);
        util.exportExcel(response, list, "企业排放水平分析数据");
    }

    /**
     * 获取企业排放水平分析详细信息
     */
    @ApiOperation(value = "详情")
    @PostMapping(value = "/{id}")
    public AjaxResults<SRaEmissionsStandard> getInfo(@PathVariable("id") String id) {
        return AjaxResults.success(sRaEmissionsStandardService.getById(id));
    }

    /**
     * 新增企业排放水平分析
     */
    @ApiOperation(value = "新增")
    @Log(title = "企业排放水平分析", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody SRaEmissionsStandard sRaEmissionsStandard) {
        return toAjax(sRaEmissionsStandardService.save(sRaEmissionsStandard));
    }

    /**
     * 修改企业排放水平分析
     */
    @ApiOperation(value = "修改")
    @Log(title = "企业排放水平分析", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody SRaEmissionsStandardVo sRaEmissionsStandardVo) {
        if (ObjectUtil.isEmpty(sRaEmissionsStandardVo)) {
            throw new InvalidParameterException("参数为空");
        }
        if (ObjectUtil.isEmpty(sRaEmissionsStandardVo.getId())) {
            throw new InvalidParameterException("参数id为空");
        }
        SRaEmissionsStandard sRaEmissionsStandard = sRaEmissionsStandardService.getById(sRaEmissionsStandardVo.getId());
        if (ObjectUtil.isEmpty(sRaEmissionsStandard)) {
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", sRaEmissionsStandardVo.getId()));
        }
        //赋值
        BeanUtil.copyProperties(sRaEmissionsStandardVo, sRaEmissionsStandard);
        return toAjax(sRaEmissionsStandardService.saveOrUpdate(sRaEmissionsStandard));
    }

    /**
     * 删除企业排放水平分析
     */
    @ApiOperation(value = "删除")
    @RequiresPermissions("talroad:standard:remove")
    @Log(title = "企业排放水平分析", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResults<Boolean> remove(@PathVariable String[] ids) {
        return toAjax(sRaEmissionsStandardService.removeByIds(Arrays.asList(ids)));
    }

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

    /**
     * 排放规模options
     */
    @ApiOperation(value = "排放规模options")
    @GetMapping("/pullEmissionScaleOptions")
    public AjaxResults<List<Map<String, String>>> pullEmissionScaleOptions() {
        return AjaxResults.success(sRaEmissionsStandardService.pullEmissionScaleOptions());
    }
}
