package cn.iocoder.yudao.module.rescue.app.controller.admin;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.rescue.core.controller.BaseController;
import cn.iocoder.yudao.module.rescue.domain.RescueInfo;
import cn.iocoder.yudao.module.rescue.dto.DriverInfo2Dto;
import cn.iocoder.yudao.module.rescue.dto.DriverInfoDto;
import cn.iocoder.yudao.module.rescue.service.IRescueInfoService;
import cn.iocoder.yudao.module.system.api.permission.PermissionApi;
import cn.iocoder.yudao.module.system.api.permission.RoleApi;
import cn.iocoder.yudao.module.system.api.permission.dto.RoleReqDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2023-07-14
 */
@RestController
@RequestMapping("/app/rescueInfo")
public class RescueInfoController extends BaseController {

    @Resource
    private AdminUserApi userService;

    @Resource
    private RoleApi roleApi;

    @Resource
    private PermissionApi permissionApi;
    @Resource
    private IRescueInfoService rescueInfoService;

    /**
     * 获取救援订单
     */
    @GetMapping("/getRescueList")
    public CommonResult getRescueList(RescueInfo rescueInfo,
                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<RescueInfo> page = new Page<>(pageNum, pageSize);
        //获取当前登录用户
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        AdminUserRespDTO user = userService.getUser(loginUser.getId());
        List<Long> roleIds = permissionApi.getRoleIdsByUserId(user.getId());
        List<RoleReqDTO> roleList = roleApi.getRoleList();
        List<RoleReqDTO> roles = roleList.stream().filter(item -> roleIds.contains(item.getId())).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(roles)) {
            for (RoleReqDTO role : roles) {
                //如果是调度中心
                if (role.getCode().equals("admin") || role.getCode().equals("ddzx") || role.getCode().equals("qt") || role.getCode().equals("kj") || role.getCode().equals("cn")) {
                    IPage<RescueInfo> rescueInfos = rescueInfoService.selectRescueInfoListByAdmin(rescueInfo, page);
                    return success(rescueInfos);
                }
            }
        }

        IPage<RescueInfo> rescueInfos = rescueInfoService.selectRescueInfoList(rescueInfo, page);
        return success(rescueInfos);
    }

    @GetMapping("/getRescueStatistics")
    public CommonResult getRescueStatistics(RescueInfo rescueInfo) {

        //获取当前登录用户
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        AdminUserRespDTO user = userService.getUser(loginUser.getId());
        List<RoleReqDTO> roleList = roleApi.getRoleList();
        List<Long> roleIdsByUserId = permissionApi.getRoleIdsByUserId(user.getId());
        if (CollectionUtil.isNotEmpty(roleIdsByUserId)) {
            List<RoleReqDTO> roles = roleList.stream().filter(item -> roleIdsByUserId.contains(item.getId())).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(roles)) {
                for (RoleReqDTO role : roles) {
                    //如果是调度中心
                    if (role.getCode().equals("admin") || role.getCode().equals("ddzx") || role.getCode().equals("qt") || role.getCode().equals("kj") || role.getCode().equals("cn")) {
                        Map<String, Object> res = rescueInfoService.getRescueStatisticsByAdmin(rescueInfo);
                        return success(res);
                    }
                }
            }
        }
        Map<String, Object> res = rescueInfoService.getRescueStatistics(rescueInfo);
        return success(res);
    }

    /**
     * 获取救援订单
     */
    @GetMapping("/rescueInfoDetail")
    public CommonResult rescueInfoDetail(Long rescueId) {
        return success(rescueInfoService.rescueInfoDetail(rescueId));
    }

    /**
     * 新增【道路救援发起】
     */
//    @Log(title = "【道路救援发起】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public CommonResult add(@RequestBody @Valid RescueInfo rescueInfo) {
        rescueInfoService.insertRescueInfo(rescueInfo);
        return success();
    }

    /**
     * 新增【道路救援发起】
     */
//    @Log(title = "【道路救援编辑】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public CommonResult edit(@RequestBody @Valid RescueInfo rescueInfo) {
        rescueInfoService.updateRescueInfo(rescueInfo);
        return success();
    }

    /**
     * 取消【道路救援】
     */
//    @Log(title = "取消【道路救援】", businessType = BusinessType.CLEAN)
    @PostMapping("/cancel")
    public CommonResult cancel(Long id) {
        RescueInfo rescueInfo = new RescueInfo();
        rescueInfo.setId(id);
        rescueInfo.setRescueStatus("0");
        return toAjax(rescueInfoService.updateRescueInfo(rescueInfo));
    }

    /**
     * 删除道路救援
     */
//    @Log(title = "删除道路救援", businessType = BusinessType.DELETE)
    @PostMapping("/delRescueInfo")
    public CommonResult delRescueInfo(Long id) {
        rescueInfoService.deleteRescueInfoById(id);
        return success();
    }
//

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/driverList")
    public CommonResult driverList(DriverInfoDto driverInfoDto) {
        List<DriverInfo2Dto> list = rescueInfoService.driverListApp(driverInfoDto);
        return success(list);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/driverInMap")
    public CommonResult driverInMap(DriverInfoDto driverInfoDto) {
        List<DriverInfo2Dto> driverList = rescueInfoService.driverInMap(driverInfoDto);
        return success(driverList);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/driverInMap2")
    public CommonResult driverInMap2() {
        return success(rescueInfoService.driverInMap2());
    }

    /**
     * 获取扣车订单
     */
    @GetMapping("/getKcList")
    public CommonResult getKcList(RescueInfo rescueInfo,
                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<RescueInfo> page = new Page<>(pageNum, pageSize);
        IPage<RescueInfo> rescueInfos = rescueInfoService.getKcList(rescueInfo, page);
        return success(rescueInfos);
    }

    /**
     * 获取扣车订单
     */
    @PostMapping("/openKc")
    public CommonResult openKc(Long rescueId) throws Exception {
        //获取当前登录用户
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        AdminUserRespDTO user = userService.getUser(loginUser.getId());
        List<RoleReqDTO> roleList = roleApi.getRoleList();
        List<Long> roleIdsByUserId = permissionApi.getRoleIdsByUserId(user.getId());
        if (CollectionUtil.isNotEmpty(roleIdsByUserId)) {
            List<RoleReqDTO> roles = roleList.stream().filter(item -> roleIdsByUserId.contains(item.getId())).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(roles)) {
                for (RoleReqDTO role : roles) {
                    //如果是交警大队
                    if (role.getCode().equals("jjdd")) {
                        rescueInfoService.openKc(rescueId);
                    }
                }
            }
        }
        return success();
    }

    /**
     * 进行还车
     */
    @PostMapping("/hcOpen")
    public CommonResult hcOpen(Long rescueId) {
        //获取当前登录用户
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        AdminUserRespDTO user = userService.getUser(loginUser.getId());
        RescueInfo rescueInfo = rescueInfoService.getById(rescueId);
        rescueInfo.setHcTime(new Date());
        rescueInfo.setHcUserId(user.getId());
        rescueInfo.setHcUserName(user.getNickname());
        rescueInfo.setHcUserPhone(user.getMobile());
        rescueInfo.setRescueStatus("8");
        rescueInfoService.updateById(rescueInfo);
        return success();
    }

    /**
     * 获取扣车订单
     */
    @GetMapping("/getHcList")
    public CommonResult getHcList(RescueInfo rescueInfo,
                                  @RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize) {
        Page<RescueInfo> page = new Page<>(pageNum, pageSize);
        IPage<RescueInfo> rescueInfos = rescueInfoService.getHcList(rescueInfo, page);
        return success(rescueInfos);
    }

    /**
     * 获取扣车订单
     */
    @GetMapping("/statisticsInfo")
    public CommonResult statisticsInfo(String type) {
        return success(rescueInfoService.statisticsInfo(type));
    }
}
