package com.willalp.web.controller.yiqing;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.page.TableDataInfo;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.StringUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.yiqing.domain.HsTaskBelongUser;
import com.willalp.yiqing.service.IHsTaskBelongUserService;
import com.willalp.yiqing.service.IHsYiqingTaskService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 疫情防控-任务负责人Controller
 *
 * @author willalp
 * @date 2022-04-02
 */
@RestController
@RequestMapping("/yiqing/taskBelong")
public class HsTaskBelongUserController extends BaseController {
    @Resource
    private IHsTaskBelongUserService hsTaskBelongUserService;

    @Resource
    private IHsYiqingTaskService taskService;

    /**
     * 查询疫情防控-任务负责人列表
     */
    @PreAuthorize("@ss.hasPermi('yiqing:taskBelong:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsTaskBelongUser hsTaskBelongUser) {
        startPage();
        List<HsTaskBelongUser> list = hsTaskBelongUserService.selectHsTaskBelongUserList(hsTaskBelongUser);
        return getDataTable(list);
    }

    /**
     * 导出疫情防控-任务负责人列表
     */
    @PreAuthorize("@ss.hasPermi('yiqing:taskBelong:export')")
    @Log(title = "疫情防控-任务负责人", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsTaskBelongUser hsTaskBelongUser) {
        List<HsTaskBelongUser> list = hsTaskBelongUserService.selectHsTaskBelongUserList(hsTaskBelongUser);
        ExcelUtil<HsTaskBelongUser> util = new ExcelUtil<HsTaskBelongUser>(HsTaskBelongUser.class);
        return util.exportExcel(list, "疫情防控-任务负责人数据");
    }

    /**
     * 获取疫情防控-任务负责人详细信息
     */
    @PreAuthorize("@ss.hasPermi('yiqing:taskBelong:query')")
    @GetMapping(value = "/{taskBelongId}")
    public AjaxResult getInfo(@PathVariable("taskBelongId") Long taskBelongId) {
        return AjaxResult.success(hsTaskBelongUserService.getById(taskBelongId));
    }

    /**
     * 新增疫情防控-任务负责人
     */
    @PreAuthorize("@ss.hasPermi('yiqing:taskBelong:add')")
    @Log(title = "疫情防控-任务负责人", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsTaskBelongUser hsTaskBelongUser) {
        Integer belongLevel = hsTaskBelongUser.getBelongLevel();
        if (0 == belongLevel) {
            String yqTaskId = hsTaskBelongUser.getYqTaskId();
            if (StringUtils.isBlank(yqTaskId)) {
                return AjaxResult.error("任务ID不能为空");
            }
            HsTaskBelongUser task = hsTaskBelongUserService.getOne(
                    new QueryWrapper<HsTaskBelongUser>().eq("yq_task_id", yqTaskId)
                            .eq("belong_level", 0));
            if (ObjectUtils.isNotEmpty(task)) {
                return AjaxResult.error("只能有一个主要负责人");
            }
        }
        while (true) {
            String idStr = IdWorker.getIdStr();
            HsTaskBelongUser byId = hsTaskBelongUserService.getById(Long.valueOf(idStr));
            if (null == byId) {
                hsTaskBelongUser.setTaskBelongId(idStr);
                break;
            }
        }
        hsTaskBelongUser.setCreateBy(SecurityUtils.getUsername());
        hsTaskBelongUser.setCreateTime(DateUtils.getNowDate());
        return toAjax(hsTaskBelongUserService.save(hsTaskBelongUser));
    }

    /**
     * 修改疫情防控-任务负责人
     */
    @PreAuthorize("@ss.hasPermi('yiqing:taskBelong:edit')")
    @Log(title = "疫情防控-任务负责人", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsTaskBelongUser hsTaskBelongUser) {
        hsTaskBelongUser.setUpdateBy(SecurityUtils.getUsername());
        hsTaskBelongUser.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsTaskBelongUserService.updateById(hsTaskBelongUser));
    }

    /**
     * 删除疫情防控-任务负责人
     */
    @PreAuthorize("@ss.hasPermi('yiqing:taskBelong:remove')")
    @Log(title = "疫情防控-任务负责人", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskBelongIds}")
    public AjaxResult remove(@PathVariable Long[] taskBelongIds) {
        return toAjax(hsTaskBelongUserService.removeByIds(Arrays.asList(taskBelongIds)));
    }
}
