package com.willalp.web.controller.yiqing;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
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.exception.base.BaseException;
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.number.domain.req.NumberRuleReq;
import com.willalp.number.service.IHsSysNumberRuleService;
import com.willalp.push.domain.HsAppPush;
import com.willalp.push.service.IHsAppPushService;
import com.willalp.yiqing.domain.HsTaskBelongUser;
import com.willalp.yiqing.domain.HsYiqingTask;
import com.willalp.yiqing.domain.vo.TaskBelongSaveVo;
import com.willalp.yiqing.service.HsYiQingService;
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.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 疫情防控-任务排班Controller
 *
 * @author willalp
 * @date 2022-04-02
 */
@RestController
@RequestMapping("/yiqing/yqTask")
public class HsYiqingTaskController extends BaseController {
    @Resource
    private IHsYiqingTaskService hsYiqingTaskService;

    @Resource
    private IHsSysNumberRuleService numberRuleService;

    @Resource
    private HsYiQingService yiQingService;

    @Resource
    private IHsTaskBelongUserService belongUserService;

    @Resource
    private IHsAppPushService pushService;

//    @Resource
//    private UniAppUtils uniAppUtils;

    /**
     * 查询疫情防控-任务排班列表
     */
    @GetMapping("/pushTask")
    public AjaxResult pushTask(@RequestParam String taskId) {
        if (StringUtils.isBlank(taskId)) {
            throw new BaseException("任务ID不能为空");
        }
        HsYiqingTask task = hsYiqingTaskService.getById(taskId);
        task.setTaskStatus(1);
        hsYiqingTaskService.updateById(task);
        List<HsTaskBelongUser> belongUsers = belongUserService.getBaseMapper()
                .selectList(new QueryWrapper<HsTaskBelongUser>()
                        .eq("yq_task_id", taskId));
        List<HsAppPush> hsAppPushes = pushService.selectHsAppPushList(null);
        List<String> userNos = new ArrayList<>();

        belongUsers.forEach(b -> {
            userNos.add(b.getBelongUserNo());
            b.setMsgIsRead(0);
            b.setUserTaskStatus(0);
            b.setIsOrder(0);
        });
        userNos.forEach(u-> hsAppPushes.forEach(p -> {
            if (StringUtils.equals(u, p.getUserNo())) {
//                uniAppUtils.pushMsgOffLine(p.getPushCid(), "测试标题", "测试内容");
            }
        }));
        belongUserService.updateBatchById(belongUsers);
        return AjaxResult.success();
    }


    /**
     * 查询疫情防控-任务排班列表
     */
    @PostMapping("/beLongSave")
    public AjaxResult beLongSave(@RequestBody TaskBelongSaveVo taskBelongSaveVo) {
        return toAjax(yiQingService.taskBelongSave(taskBelongSaveVo));
    }


    /**
     * 查询疫情防控-任务排班列表
     */
    @PreAuthorize("@ss.hasPermi('yiqing:yqTask:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsYiqingTask hsYiqingTask) {
        startPage();
        List<HsYiqingTask> list = hsYiqingTaskService.selectHsYiqingTaskList(hsYiqingTask);
        return getDataTable(list);
    }

    /**
     * 导出疫情防控-任务排班列表
     */
    @PreAuthorize("@ss.hasPermi('yiqing:yqTask:export')")
    @Log(title = "疫情防控-任务排班", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsYiqingTask hsYiqingTask) {
        List<HsYiqingTask> list = hsYiqingTaskService.selectHsYiqingTaskList(hsYiqingTask);
        ExcelUtil<HsYiqingTask> util = new ExcelUtil<HsYiqingTask>(HsYiqingTask.class);
        return util.exportExcel(list, "疫情防控-任务排班数据");
    }

    /**
     * 获取疫情防控-任务排班详细信息
     */
    @PreAuthorize("@ss.hasPermi('yiqing:yqTask:query')")
    @GetMapping(value = "/{yqTaskId}")
    public AjaxResult getInfo(@PathVariable("yqTaskId") String yqTaskId) {
        return AjaxResult.success(hsYiqingTaskService.getById(yqTaskId));
    }

    /**
     * 新增疫情防控-任务排班
     */
    @PreAuthorize("@ss.hasPermi('yiqing:yqTask:add')")
    @Log(title = "疫情防控-任务排班", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsYiqingTask hsYiqingTask) {
        String idStr = null;
        while (true) {
            idStr = IdWorker.getIdStr();
            HsYiqingTask byId = hsYiqingTaskService.getById(Long.valueOf(idStr));
            if (null == byId) {
                hsYiqingTask.setYqTaskId(idStr);
                break;
            }
        }
        NumberRuleReq numberRuleReq = new NumberRuleReq("17");
        numberRuleReq.setDate(new Date());
        String number = numberRuleService.getNumber(numberRuleReq);
        hsYiqingTask.setTaskNo(number);
        hsYiqingTask.setCreateBy(SecurityUtils.getUsername());
        hsYiqingTask.setCreateTime(DateUtils.getNowDate());
        if (!hsYiqingTaskService.save(hsYiqingTask)) {
            return AjaxResult.error("操作失败");
        }
        return AjaxResult.success(hsYiqingTask);
    }

    /**
     * 修改疫情防控-任务排班
     */
    @PreAuthorize("@ss.hasPermi('yiqing:yqTask:edit')")
    @Log(title = "疫情防控-任务排班", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsYiqingTask hsYiqingTask) {
        hsYiqingTask.setUpdateBy(SecurityUtils.getUsername());
        hsYiqingTask.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsYiqingTaskService.updateById(hsYiqingTask));
    }

    /**
     * 删除疫情防控-任务排班
     */
    @PreAuthorize("@ss.hasPermi('yiqing:yqTask:remove')")
    @Log(title = "疫情防控-任务排班", businessType = BusinessType.DELETE)
    @DeleteMapping("/{yqTaskIds}")
    public AjaxResult remove(@PathVariable String[] yqTaskIds) {
        return toAjax(hsYiqingTaskService.removeByIds(Arrays.asList(yqTaskIds)));
    }
}
