package cn.cecep.talroad.controller.effective;

import cn.cecep.talroad.common.core.constant.HttpStatus;
import cn.cecep.talroad.common.core.domain.R;
import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
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.utils.SecurityUtils;
import cn.cecep.talroad.domain.effective.RectificationEffectQueryDTO;
import cn.cecep.talroad.feign.BaseRegionService;
import cn.cecep.talroad.service.effective.IRectificationEffectEvaluationService;
import cn.cecep.talroad.vo.AmProblemFiVo;
import cn.cecep.talroad.vo.effective.FactoryRectificationEffectCountVO;
import cn.cecep.talroad.vo.query.AmProblemFiQuery;
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.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 整改效果评估控制层
 */
@RestController
@RequestMapping("/rectificationEffect")
@Api(tags = "整改效果评估控制层")
public class RectificationEffectEvaluationController {

    @Autowired
    private BaseRegionService baseRegionService;

    @Autowired
    private IRectificationEffectEvaluationService rectificationEffectEvaluationService;


    @PostMapping("/list")
    @ApiOperation(value = "查询列表")
    public AjaxResults<Page<FactoryRectificationEffectCountVO>> list(@RequestBody RectificationEffectQueryDTO rectificationEffectQueryDTO){
        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();
        }
        if(StringUtils.isBlank(rectificationEffectQueryDTO.getRegionCode())){
            rectificationEffectQueryDTO.setRegionCodes(regionCodes);
        }
        return rectificationEffectEvaluationService.list(rectificationEffectQueryDTO);
    }

    @ApiOperation(value = "导出列表")
    @Log(title = "导出列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody RectificationEffectQueryDTO rectificationEffectQueryDTO) {
        AjaxResults<Page<FactoryRectificationEffectCountVO>> results = rectificationEffectEvaluationService.list(rectificationEffectQueryDTO);
        if(results.getCode() == HttpStatus.SUCCESS){
            ExcelUtil<FactoryRectificationEffectCountVO> util = new ExcelUtil<>(FactoryRectificationEffectCountVO.class);
            util.exportExcel(response, results.getData().getRecords(), "整改效果评估数据");
        }else{
            throw new RuntimeException("导出异常");
        }
    }
}
