package com.zd.bottle.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zd.bottle.domain.UsegasApply;
import com.zd.bottle.domain.UsegasAuditRecord;
import com.zd.bottle.service.UsegasApplyService;
import com.zd.bottle.service.UsegasAuditRecordService;
import com.zd.bottle.vo.UsegasApplyDetailVo;
import com.zd.bottle.vo.UsegasApplyTabVo;
import com.zd.bottle.vo.UsegasApplyVo;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.common.security.service.TokenService;
import com.zd.common.security.utils.SaveUtil;
import com.zd.system.api.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用气申请表 前端控制器
 * </p>
 *
 * @author hanson
 * @since 2022-09-08
 */
@RestController
@Api(tags = "【用气申请表】")
@RequestMapping("/usegasApply")
public class UsegasApplyController extends BaseController<UsegasApply> {

    @Autowired
    private UsegasApplyService usegasApplyService;
    @Autowired
    private UsegasAuditRecordService usegasAuditRecordService;
    @Autowired
    private TokenService tokenService;

    @ApiModelProperty(value = "待审核")
    @PreAuthorize(hasPermi = PerPrefix.QP_USEGASAPPLY + PerFun.LIST)
    @GetMapping("/waitApplyList")
    public TableDataInfo <UsegasApply> waitApplyList(UsegasApply usegasApply){
        startPage();
        usegasApply.setLeadAuditStaus((short) 0);
        List <UsegasApplyVo> list = usegasApplyService.selectUseagsApplyList(usegasApply);
        return getDataTable(list);
    }

    @ApiModelProperty(value = "已通过")
    @PreAuthorize(hasPermi = PerPrefix.QP_USEGASAPPLY + PerFun.LIST)
    @GetMapping("/passApplyList")
    public TableDataInfo <UsegasApply> passApplyList(UsegasApply usegasApply){
        startPage();
        usegasApply.setLeadAuditStaus((short) 1);
        List <UsegasApplyVo> list = usegasApplyService.selectUseagsApplyList(usegasApply);
        return getDataTable(list);
    }

    @ApiModelProperty(value = "未通过")
    @PreAuthorize(hasPermi = PerPrefix.QP_USEGASAPPLY + PerFun.LIST)
    @GetMapping("/refuseApplyList")
    public TableDataInfo <UsegasApply> refuseApplyList(UsegasApply usegasApply){
        startPage();
        usegasApply.setLeadAuditStaus((short) 2);
        List <UsegasApplyVo> list = usegasApplyService.selectUseagsApplyList(usegasApply);
        return getDataTable(list);
    }


    @ApiModelProperty(value = "审批选项卡统计")
    @GetMapping("/applyTabCount")
    public ResultData<UsegasApplyTabVo> selectApplyTabCount(UsegasApply usegasApply){
        UsegasApplyTabVo usegasApplyTabVo = usegasApplyService.selectApplyTabCount(usegasApply);
        return ResultData.success(usegasApplyTabVo);
    }


    /**---------------------------------- 用气申请 -------------------------------------*/

    /**
     * 获取当前登录用户信息
     */
    @ApiOperation(value = "获取当前登录用户信息")
    @GetMapping("/getLoginUser")
    public ResultData<SysUser> getLoginUser(){
        //获取当前登录用户信息
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        return ResultData.success(sysUser);
    }

    @ApiOperation(value = "个人申请列表")
    @PostMapping("/listByUser")
    public TableDataInfo<UsegasApply> queryApplyListByUser(@RequestBody UsegasApply usegasApply){

        startPage();
        QueryWrapper<UsegasApply> wrapper = new QueryWrapper();
        wrapper.eq("user_id", SecurityUtils.getUserId());
        if(usegasApply.getLeadAuditStaus() != null){
            wrapper.eq("lead_audit_staus", usegasApply.getLeadAuditStaus());
        }
        wrapper.orderByDesc("create_time");
        List<UsegasApply> list = usegasApplyService.list(wrapper);
        return getDataTable(list);
    }

    @ApiOperation(value = "个人提交申请")
    @PostMapping("/addByUser")
    public ResultData<UsegasApplyTabVo> addApplyByUser(@RequestBody @Valid UsegasApply usegasApply){
        SaveUtil.setCommonAttr(usegasApply);
        usegasApplyService.save(usegasApply);
        return ResultData.success();
    }

    @ApiOperation(value = "个人重新申请")
    @PostMapping("/updateByUser")
    public ResultData<UsegasApplyTabVo> updateApplyByUser(@RequestBody @Valid UsegasApply usegasApply){
        usegasApply.setUpdateBy(SecurityUtils.getUsername());
        usegasApply.setUpdateTime(DateUtils.getNowDate());
        usegasApply.setLeadAuditStaus((short) 0);
        usegasApplyService.updateById(usegasApply);
        return ResultData.success();
    }

    @ApiOperation(value = "申请详情")
    @PostMapping("/detail")
    public ResultData<UsegasApplyDetailVo> queryApplyDetail(@RequestBody UsegasApply usegasApply){

        UsegasApplyDetailVo usegasApplyDetailVo = new UsegasApplyDetailVo();

        usegasApply = usegasApplyService.getById(usegasApply.getId());

        if(usegasApply == null){
            return ResultData.fail("未查询到数据！");
        }

        BeanUtils.copyProperties(usegasApply, usegasApplyDetailVo);

        QueryWrapper<UsegasAuditRecord> queryWrapper = new QueryWrapper();

        queryWrapper.eq("usegas_apply_id", usegasApplyDetailVo.getId());
        List<UsegasAuditRecord> list = usegasAuditRecordService.list(queryWrapper);

        usegasApplyDetailVo.setDetails(list);

        return ResultData.success(usegasApplyDetailVo);
    }
}
