package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.exception.CommonException;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.cecep.talroad.vo.query.BMainThreeSupervisionEnforcQuery;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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 java.security.InvalidParameterException;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.core.web.controller.BasesController;
import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.domain.BMainThreeSupervisionEnforc;
import cn.cecep.talroad.vo.BMainThreeSupervisionEnforcVo;
import cn.cecep.talroad.service.IBMainThreeSupervisionEnforcService;


/**
 * * 开发公司：中节能天融科技有限公司
 * 三监联动执法信息Controller 控制层
 *
 * @author szhpt
 * @date 2023-03-29*/
@Api(tags = "三监联动执法信息")
@RestController
@RequestMapping("/threeSupervision")
public class BMainThreeSupervisionEnforcController extends BasesController
{
    @Autowired
    private IBMainThreeSupervisionEnforcService bMainThreeSupervisionEnforcService;

    /**
     * 查询三监联动执法信息列表
     */
    @ApiOperation(value = "查询三监联动执法信息")
    @GetMapping("/list")
    public AjaxResults<IPage<BMainThreeSupervisionEnforc>> list(BMainThreeSupervisionEnforcQuery bMainThreeSupervisionEnforc)
    {
        Page<BMainThreeSupervisionEnforc> page = new Page<>(bMainThreeSupervisionEnforc.getPageNum(), bMainThreeSupervisionEnforc.getPageSize());
        IPage<BMainThreeSupervisionEnforc> list = bMainThreeSupervisionEnforcService.page(page, BMainThreeSupervisionEnforcQuery.createLambdaQueryWrapper(bMainThreeSupervisionEnforc));
        return AjaxResults.success(list);
    }


    /**
     * 导出三监联动执法信息列表
     */
    @ApiOperation(value = "导出三监联动执法信息")
    @Log(title = "三监联动执法信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody BMainThreeSupervisionEnforcQuery bMainThreeSupervisionEnforc)
    {
        List<BMainThreeSupervisionEnforc> list = bMainThreeSupervisionEnforcService.list(BMainThreeSupervisionEnforcQuery.createLambdaQueryWrapper(bMainThreeSupervisionEnforc));
        ExcelUtil<BMainThreeSupervisionEnforc> util = new ExcelUtil<BMainThreeSupervisionEnforc>(BMainThreeSupervisionEnforc.class);
        util.exportExcel(response, list, "三监联动执法信息数据");
    }

    /**
     * 导出三监联动执法信息列表
     */
    @ApiOperation(value = "发起部门下拉框")
    @GetMapping("/initiatingDeptSelectList")
    public List<BMainThreeSupervisionEnforc> initiatingDeptSelectList() {
        QueryWrapper<BMainThreeSupervisionEnforc> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("distinct initiating_dept_id, initiating_dept_name")
                        .orderByAsc("initiating_dept_name");
        return bMainThreeSupervisionEnforcService.list(queryWrapper);
    }

    /**
     * 获取三监联动执法信息详细信息
     */
    @ApiOperation(value = "获取三监联动执法信息详细信息")
    @GetMapping(value = "/{taskId}")
    public AjaxResults<BMainThreeSupervisionEnforcVo> getInfo(@PathVariable("taskId") String taskId) {
        return AjaxResults.success(bMainThreeSupervisionEnforcService.selectById(taskId));
    }
    /**
     * 新增三监联动执法信息
     */
    @ApiOperation(value = "新增三监联动执法信息")
    @Log(title = "三监联动执法信息", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody BMainThreeSupervisionEnforc bMainThreeSupervisionEnforc) {
        return toAjax(bMainThreeSupervisionEnforcService.save(bMainThreeSupervisionEnforc));
    }
    /**
     * 修改三监联动执法信息
     */
    @ApiOperation(value = "修改三监联动执法信息")
    @Log(title = "三监联动执法信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody BMainThreeSupervisionEnforcVo bMainThreeSupervisionEnforcVo) {
        if(ObjectUtil.isEmpty(bMainThreeSupervisionEnforcVo)){
            throw new InvalidParameterException("参数为空");
        }
        if(ObjectUtil.isEmpty(bMainThreeSupervisionEnforcVo.getTaskId())){
            throw new InvalidParameterException("参数id为空");
        }
        BMainThreeSupervisionEnforc bMainThreeSupervisionEnforc = bMainThreeSupervisionEnforcService.getById(bMainThreeSupervisionEnforcVo.getTaskId());
        if(ObjectUtil.isEmpty(bMainThreeSupervisionEnforc)){
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", bMainThreeSupervisionEnforcVo.getTaskId()));
        }
        //赋值
        BeanUtil.copyProperties(bMainThreeSupervisionEnforcVo,bMainThreeSupervisionEnforc);
        return toAjax(bMainThreeSupervisionEnforcService.saveOrUpdate(bMainThreeSupervisionEnforc));
    }

    /**
     * 删除三监联动执法信息
     */
    @ApiOperation(value = "删除三监联动执法信息")
    @RequiresPermissions("talroad:enforc:remove")
    @Log(title = "三监联动执法信息", businessType = BusinessType.DELETE)
    @PostMapping("/{taskIds}")
    public AjaxResults<Boolean> remove(@PathVariable String[] taskIds) {
        return toAjax(bMainThreeSupervisionEnforcService.removeByIds(Arrays.asList(taskIds)));
    }
    @ApiOperation(value = "第三方三监联动执法信息列表")
    @PostMapping("/get")
    public Object get(@RequestBody Map data, HttpServletRequest request) {
        return bMainThreeSupervisionEnforcService.xianheGet(data,request.getHeader("Authorization"));
    }
    @ApiOperation(value = "第三方三监联动执法信息详情")
    @GetMapping("/getXianHeInfo")
    public Object getInfo(String taskId,HttpServletRequest request) {
        return bMainThreeSupervisionEnforcService.xianheGetInfo(taskId,request.getHeader("Authorization"));
    }
    @ApiOperation(value = "第三方企业核查列表")
    @PostMapping("/getTaskAccount")
    public Object getTaskAccount(@RequestBody Map data, HttpServletRequest request) {
        return bMainThreeSupervisionEnforcService.xianheGetTaskAccount(data,request.getHeader("Authorization"));
    }
    @ApiOperation(value = "第三方企业核查详情")
    @GetMapping("/getTaskInfo")
    public Object getTaskInfo(String taskId,HttpServletRequest request) {
        return bMainThreeSupervisionEnforcService.xianheGetTaskInfo(taskId,request.getHeader("Authorization"));
    }
    @ApiOperation(value = "第三方获取区县")
    @GetMapping("/getCityCodeList")
    public Object getCityCodeList(String pid,HttpServletRequest request) {
        return bMainThreeSupervisionEnforcService.xianheGetCityCodeList(pid,request.getHeader("Authorization"));
    }
    @ApiOperation(value = "第三方企业核查列表")
    @PostMapping("/getCheckTaskAccount")
    public Object getCheckTaskAccount(@RequestBody Map data, HttpServletRequest request) {
        return bMainThreeSupervisionEnforcService.xianheGetCheckTaskAccount(data,request.getHeader("Authorization"));
    }
    @ApiOperation(value = "第三方动态传递")
    @PostMapping("/getAllUrl")
    public Object getAllUrl(@RequestBody XianheInfo xianheInfo,HttpServletRequest request) {
        return bMainThreeSupervisionEnforcService.xianheGetAllUrl(xianheInfo.getUrl()
                ,xianheInfo.method,xianheInfo.param,request.getHeader("Authorization"));
    }
    @Data
    static class XianheInfo{
        private String url;
        private String param;
        private String method;
    }
}
