package com.izouma.nineth.web;

import com.izouma.nineth.domain.User;
import com.izouma.nineth.domain.UserWareHouse;
import com.izouma.nineth.domain.WhiteListConfigInfo;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.dto.UserWareHouseExcelDTO;
import com.izouma.nineth.enums.ItemType;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.UserRepo;
import com.izouma.nineth.repo.UserWareHouseRepo;
import com.izouma.nineth.service.UserWareHouseService;
import com.izouma.nineth.service.WhitelistConfigInfoService;
import com.izouma.nineth.utils.JpaUtils;
import com.izouma.nineth.utils.ObjUtils;
import com.izouma.nineth.utils.excel.ExcelUtils;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/userWareHouse")
@AllArgsConstructor
public class UserWareHouseController extends BaseController {

    private UserWareHouseRepo userWareHouseRepo;
    private UserRepo userRepo;
    private WhitelistConfigInfoService whitelistConfigInfoService;

    private UserWareHouseService userWareHouseService;



    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/all")
    public Page<UserWareHouse> all(@RequestBody PageQuery pageQuery) {
        Page<UserWareHouse> page = userWareHouseService.all(pageQuery);
        return page;

    }

    @GetMapping("/get/{id}")
    public UserWareHouse get(@PathVariable Long id) {
        UserWareHouse e = userWareHouseRepo.findById(id).orElseThrow(new BusinessException("无记录"));
        return e;
    }

    @PostMapping("/save")
    @PreAuthorize("hasRole('ADMIN')")
    public UserWareHouse save(@RequestBody UserWareHouse otd) {
        if (otd.getId() != null) {
            UserWareHouse orig = userWareHouseRepo.findById(otd.getId()).orElseThrow(new BusinessException("无记录"));
            otd.setItemName(ItemType.getValue(orig.getItemId()));
            ObjUtils.merge(orig, otd);
            return userWareHouseRepo.save(orig);
        } else {
            throw new BusinessException("只能从批量增加那里插入数据,不支持编辑");
        }
    }


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

    @PostMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, @RequestBody PageQuery pageQuery) throws IOException {
        Page<UserWareHouse> page = userWareHouseService.all(pageQuery);
        List<UserWareHouseExcelDTO> list = page.getContent().stream().map(UserWareHouseExcelDTO::new).collect(Collectors.toList());
        ExcelUtils.export(response, list);
    }


    /**
     * 清理惊喜盒子
     */
    @GetMapping("/cleanSurpriseBox")
    @PreAuthorize("hasRole('ADMIN')")
    public void cleanSurpriseBox() {
        userWareHouseRepo.cleanSurpriseBox();
    }

    /**
     * 清理优先购
     */
    @GetMapping("/cleanPreBuy")
    @PreAuthorize("hasRole('ADMIN')")
    public void cleanPreBuy() {
        userWareHouseRepo.cleanPreBuy();
    }
}