package com.ho.business.controller;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ho.business.entity.IndustrialElecRegion;
import com.ho.business.mapper.IndustrialElecRegionMapper;
import com.ho.business.service.IndustrialElecRegionService;
import com.ho.business.vo.req.industrialElec.IndustrialElecRegionReqVo;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.util.PageResult;
import com.ho.common.tools.util.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping(ContextConstant.BUSINESS + "industrialElecRegion")
@RestController
@Slf4j
@Api(tags = "业务模块-代理电价区域")
public class IndustrialElecRegionController {

    @Autowired
    private IndustrialElecRegionMapper industrialElecRegionMapper;

    @Autowired
    private IndustrialElecRegionService industrialElecRegionService;

    @PostMapping("page")
    @ApiOperation(value = "查询所有代理电价区域-分页")
    public DataResult<PageResult<IndustrialElecRegion>> page(@RequestBody IndustrialElecRegionReqVo reqVo) {
        PageHelper.startPage(reqVo.getPageNum(), reqVo.getPageSize());
        List<IndustrialElecRegion> industrialElecPrices = industrialElecRegionMapper.selectIndustElecRegion(reqVo);
        return DataResult.success(PageUtils.getPageResult(new PageInfo<>(industrialElecPrices)));
    }

    @PostMapping("list")
    @ApiOperation(value = "查询所有代理电价区域-列表")
    public DataResult<IndustrialElecRegion> list() {
        List<IndustrialElecRegion> industrialElecPrices = industrialElecRegionMapper.findAllList();
        return DataResult.success(industrialElecPrices);
    }

    @PostMapping("add")
    @ApiOperation(value = "新增代理电价区域")
    public DataResult add(@RequestBody @Valid IndustrialElecRegion industrialElecRegion) {
        if(industrialElecRegion!=null){
            IndustrialElecRegion industrial= industrialElecRegionMapper.findByRegionName(industrialElecRegion.getRegionName());
            if(industrial==null){
                industrialElecRegion.setCreateTime(new Date());
                industrialElecRegionMapper.insert(industrialElecRegion);
            }else{
                return DataResult.getResult(BaseResponseCode.REGION_NAME_ALREADY_EXISTS);
            }
        }
        return DataResult.success();
    }

    @PostMapping("/deleteById")
    @ApiOperation(value = "删除代理电价区域")
    public DataResult deleteById(@RequestBody IndustrialElecRegion industrialElecRegion) {
        industrialElecRegionMapper.deleteById(industrialElecRegion.getId());
        return DataResult.success();
    }

    @GetMapping("/findById/{id}")
    @ApiOperation(value = "根据id显示详情")
    public DataResult findById(@PathVariable("id")Integer id) {
        IndustrialElecRegion industrialElecRegion = industrialElecRegionMapper.findById(id);
        return DataResult.success(industrialElecRegion);
    }

    @PostMapping("/updateById")
    @ApiOperation(value = "根据id更新")
    public DataResult updateById(@RequestBody @Valid IndustrialElecRegion industrialElecRegion) {
        industrialElecRegion.setUpdateTime(new Date());
        industrialElecRegionMapper.updateById(industrialElecRegion);
        return DataResult.success();
    }

    @PostMapping("/addressResolution")
    @ApiOperation(value = "详细地址识别")
    public DataResult addressResolution(@RequestParam("address") String address) {
        DataResult dataResult = industrialElecRegionService.addressResolution(address);
        return dataResult;
    }
}
