package com.willalp.web.controller.yiqing;

import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.yiqing.domain.HsYiqingAddr;
import com.willalp.yiqing.service.IHsYiqingAddrService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 疫情地理地址Controller
 *
 * @author willalp
 * @date 2022-05-15
 */
@RestController
@RequestMapping("/yiqing/addr")
public class HsYiqingAddrController extends BaseController {
    @Resource
    private IHsYiqingAddrService hsYiqingAddrService;

    /**
     * 查询疫情地理地址列表
     */
    @PreAuthorize("@ss.hasPermi('yiqing:addr:list')")
    @GetMapping("/list")
    public AjaxResult list(HsYiqingAddr hsYiqingAddr) {
        List<HsYiqingAddr> list = hsYiqingAddrService.selectHsYiqingAddrList(hsYiqingAddr);
        return AjaxResult.success(list);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<HsYiqingAddr> util = new ExcelUtil<>(HsYiqingAddr.class);
        return util.importTemplateExcel("属地社区登记导入模板");
    }

    /**
     * 导出疫情地理地址列表
     */
    @PreAuthorize("@ss.hasPermi('yiqing:addr:export')")
    @Log(title = "属地社区登记", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsYiqingAddr hsYiqingAddr) {
        List<HsYiqingAddr> list = hsYiqingAddrService.selectHsYiqingAddrList(hsYiqingAddr);
        ExcelUtil<HsYiqingAddr> util = new ExcelUtil<>(HsYiqingAddr.class);
        return util.exportExcel(list, "属地社区登记");
    }

    @Log(title = "属地社区登记", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<HsYiqingAddr> util = new ExcelUtil<>(HsYiqingAddr.class);
        List<HsYiqingAddr> list = util.importExcel(file.getInputStream());
        return AjaxResult.success(hsYiqingAddrService.importData(list));
    }

    /**
     * 获取疫情地理地址详细信息
     */
    @PreAuthorize("@ss.hasPermi('yiqing:addr:query')")
    @GetMapping(value = "/{addrId}")
    public AjaxResult getInfo(@PathVariable("addrId") String addrId) {
        return AjaxResult.success(hsYiqingAddrService.getById(addrId));
    }

    /**
     * 新增疫情地理地址
     */
    @PreAuthorize("@ss.hasPermi('yiqing:addr:add')")
    @Log(title = "疫情地理地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsYiqingAddr hsYiqingAddr) {
        String addrNo = hsYiqingAddr.getAddrNo();
        if (StringUtils.isBlank(addrNo)) {
            return AjaxResult.error("地址编号不能为空");
        }
        hsYiqingAddr.setAddrTreeId(addrNo);
        hsYiqingAddr.setCreateBy(SecurityUtils.getUsername());
        hsYiqingAddr.setCreateTime(DateUtils.getNowDate());
        return toAjax(hsYiqingAddrService.save(hsYiqingAddr));
    }

    /**
     * 修改疫情地理地址
     */
    @PreAuthorize("@ss.hasPermi('yiqing:addr:edit')")
    @Log(title = "疫情地理地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsYiqingAddr hsYiqingAddr) {
        hsYiqingAddr.setUpdateBy(SecurityUtils.getUsername());
        hsYiqingAddr.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsYiqingAddrService.updateById(hsYiqingAddr));
    }

    /**
     * 删除疫情地理地址
     */
    @PreAuthorize("@ss.hasPermi('yiqing:addr:remove')")
    @Log(title = "疫情地理地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/{addrIds}")
    public AjaxResult remove(@PathVariable String[] addrIds) {
        return toAjax(hsYiqingAddrService.removeByIds(Arrays.asList(addrIds)));
    }
}
