package cn.com.ty.lift.ud.coupon.controller;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.service.IService;

import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.common.DelReq;
import cn.com.ty.lift.ud.coupon.controller.query.CouponQuery;
import cn.com.ty.lift.ud.coupon.mapper.entity.Coupon;
import cn.com.ty.lift.ud.coupon.service.ICouponService;
import cn.com.ty.lift.ud.userInfo.mapper.entity.Role;
import cn.com.ty.lift.ud.userInfo.mapper.entity.UserInfoEntity;
import cn.com.ty.lift.ud.userInfo.service.IUserInfoService;
import cn.com.ty.lift.ud.userInfo.service.RoleService;
import cn.com.ty.lift.ud.utils.CurrentUserInfo;
import cn.com.xwy.boot.web.dto.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 卡券 API
 * 
 * @author Xson
 * @date 2020-05-08
 */
@Api(tags = "卡券 API -魏瑞锋")
@RestController
@RequestMapping("/api/coupons")
public class CouponController extends BaseController<Coupon, CouponQuery> {

    private final Logger log = LoggerFactory.getLogger(CouponController.class);

    private @Autowired ICouponService couponService;

    private @Autowired IUserInfoService iUserInfoService;

    private @Autowired RoleService roleService;

    @Override
    protected IService<Coupon> getService() {
        return couponService;
    }

    // 新建
    @PostMapping("/insert")
    @ApiOperation(value = "新增")
    public RestResponse<Coupon> create(@RequestBody Coupon entity) {
        entity.setScenario(1);
        boolean saveFlag = getService().save(entity);
        if (saveFlag)
            return RestResponse.success(null, "Created successfully");
        else
            return RestResponse.fail("Created unsuccessfully");
    }

    @ApiOperation(value = "批量启用")
    @PostMapping("useBath")
    public RestResponse useBath(@RequestBody DelReq ids) {
        couponService.updateByIds(ids.getIds(), 1);
        return RestResponse.success("启用成功");
    }

    @ApiOperation(value = "批量关闭")
    @PostMapping("noUseBath")
    public RestResponse noUseBath(@RequestBody DelReq ids) {
        couponService.updateByIds(ids.getIds(), 0);
        return RestResponse.success("关闭成功");
    }

    @ApiOperation(value = "审批 1:通过 0:驳回")
    @PostMapping("/examine")
    public RestResponse examine(@RequestBody Coupon neDto) {
        String userMobile = CurrentUserInfo.userMobile();
        UserInfoEntity userByMobile = iUserInfoService.getUserByMobile(userMobile);
        if (userByMobile != null) {
            List<Role> byUserIdList = roleService.getByUserId(userByMobile.getUserId());
            for (Role byUserId : byUserIdList) {
                if (byUserId.getCode().equals("REGION_DIRECTOR")) {// 区域主管审核
                    if (neDto.getCheckFalg() == 1) {
                        return RestResponse.success("审批成功，等待财务审核");
                    } else {
                        couponService.updateSatus(neDto.getId(), 0, 0);
                        return RestResponse.success("驳回成功");
                    }
                }
                if (byUserId.getCode().equals("FINANCE")) {// 财务审核
                    if (neDto.getCheckFalg() == 1) {
                        couponService.updateSatus(neDto.getId(), 1, 1);
                        return RestResponse.success("审批通过");
                    } else {
                        couponService.updateSatus(neDto.getId(), 0, 0);
                        return RestResponse.success("驳回成功");
                    }
                }
            }
        } else {
            return RestResponse.fail("请重新登录");
        }
        return RestResponse.fail("权限不足");

    }

    // 修改
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    public RestResponse update(@RequestBody Coupon request) {
        Coupon byId = couponService.getById(request.getId());
        if (byId.getValidity() != null && byId.getValidity().before(new Date())) {
            return RestResponse.fail("已过期的红包或卡券，不能修改其状态");
        }
        if (byId.getCheckFalg() == 0) {
            request.setCheckFalg(2);
        }
        getService().updateById(request);
        return RestResponse.success(null, "Updated successfully");
    }
}