package com.izouma.jiashanxia.web;

import com.izouma.jiashanxia.domain.UserPackageFlow;
import com.izouma.jiashanxia.dto.UserPackageFlowVO;
import com.izouma.jiashanxia.dto.WriteOffRecordDTO;
import com.izouma.jiashanxia.dto.WriteOffSaveVO;
import com.izouma.jiashanxia.service.UserPackageFlowService;
import com.izouma.jiashanxia.dto.PageQuery;
import com.izouma.jiashanxia.exception.BusinessException;
import com.izouma.jiashanxia.repo.UserPackageFlowRepo;
import com.izouma.jiashanxia.utils.ObjUtils;
import com.izouma.jiashanxia.utils.SecurityUtils;
import com.izouma.jiashanxia.utils.excel.ExcelUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/userPackageFlow")
@AllArgsConstructor
public class UserPackageFlowController extends BaseController {
    private UserPackageFlowService userPackageFlowService;
    private UserPackageFlowRepo    userPackageFlowRepo;

    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public UserPackageFlow save(@RequestBody UserPackageFlow record) {
        if (record.getId() != null) {
            UserPackageFlow orig = userPackageFlowRepo.findById(record.getId())
                    .orElseThrow(new BusinessException("无记录"));
            ObjUtils.merge(orig, record);
            return userPackageFlowRepo.save(orig);
        }
        return userPackageFlowRepo.save(record);
    }


    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/all")
    public Page<UserPackageFlow> all(@RequestBody PageQuery pageQuery) {
        return userPackageFlowService.all(pageQuery);
    }

    @GetMapping("/get/{id}")
    public UserPackageFlow get(@PathVariable Long id) {
        return userPackageFlowRepo.findById(id).orElseThrow(new BusinessException("无记录"));
    }

    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        userPackageFlowRepo.softDelete(id);
    }

    @GetMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
        List<UserPackageFlow> data = all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }

    @GetMapping("/my")
    public List<UserPackageFlowVO> my() {
        return userPackageFlowService.my(SecurityUtils.getAuthenticatedUser().getId());
    }

    @GetMapping("/getDTO/{id}")
    public WriteOffRecordDTO getDTO(@PathVariable Long id) {
        return userPackageFlowService.getDTO(id);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/writeOffAll")
    @ApiOperation("核销记录")
    public Page<UserPackageFlow> writeOffAll(@RequestBody PageQuery pageQuery) {
        return userPackageFlowService.writeOffAll(pageQuery);
    }

    @PreAuthorize("hasAnyRole('ADMIN', 'ROLE_WRITER')")
    @PostMapping("/writeOffAll1")
    @ApiOperation("核销记录")
    public Page<UserPackageFlowVO> writeOffAll1(@RequestBody PageQuery pageQuery) {
        return userPackageFlowService.writeOffAll1(pageQuery);
    }

    @PreAuthorize("hasAnyRole('ADMIN','WRITER')")
    @PostMapping("/writeOff")
    @ApiOperation("核销")
    public UserPackageFlow writeOff(@RequestBody WriteOffSaveVO vo) {
        return userPackageFlowService.writeOff(vo);
    }

}

