package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.store.LifeCoupon;
import shop.alien.entity.store.vo.StoreInfoVo;
import shop.alien.mapper.LifeCouponMapper;
import shop.alien.mapper.StoreInfoMapper;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 套餐
 */
@Api(tags = {"一期-平台套餐"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/pingtaitaocan")
@RequiredArgsConstructor
public class PingtaiTaocanController {

    private final LifeCouponMapper lifeCouponMapper;

    private final StoreInfoMapper storeInfoMapper;

    @GetMapping("getTaocanList")
    private Map<String, Object> getTaocanList(@RequestParam(value = "page", defaultValue = "1") int page,
                                              @RequestParam(value = "size", defaultValue = "10") int size,
                                              @RequestParam("storeName") String storeName,
                                              @RequestParam("status") String status) {
        log.info("LifeCouponController.getTaocanList?page={},size={},storeName={},status={}", page, size, storeName, status);
        Map<String, Object> returnMap = new HashMap<>();
        IPage<LifeCoupon> quanPage = new Page<>(page, size);
        LambdaUpdateWrapper<LifeCoupon> wrapper = new LambdaUpdateWrapper<>();
        if (storeName != null && !storeName.isEmpty()) {
            QueryWrapper<StoreInfoVo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("a.store_name", storeName).eq("a.delete_flag", 0);
            List<StoreInfoVo> storeInfoVoList = storeInfoMapper.getStoreInfoVoList(queryWrapper);
            if (storeInfoVoList.isEmpty()) {
                return returnMap;
            }
            List<Integer> storeIds = storeInfoVoList.stream().map(StoreInfoVo::getId).collect(Collectors.toList());
            wrapper.in(LifeCoupon::getStoreId, storeIds);
        }
        if (status != null && !status.isEmpty()) {
            if (status.equals("0")) {
                List<Integer> statuss = new ArrayList<>();
                statuss.add(0);
                statuss.add(1);
                statuss.add(2);
                statuss.add(3);
                wrapper.in(LifeCoupon::getStatus, statuss);
            } else {
                wrapper.eq(LifeCoupon::getStatus, Integer.valueOf(status));
            }
        }
        wrapper.eq(LifeCoupon::getType, 2);
        wrapper.orderByDesc(LifeCoupon::getUpdatedTime);
        IPage<LifeCoupon> lifeyouhuiquans = lifeCouponMapper.selectPage(quanPage, wrapper);
        int totalCnt = lifeCouponMapper.selectCount(wrapper);
        if (lifeyouhuiquans.getRecords().isEmpty()) {
            return returnMap;
        }
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (LifeCoupon taocan : lifeyouhuiquans.getRecords()) {
            Map<String, Object> map = new HashMap<>();
            QueryWrapper<StoreInfoVo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("a.id", taocan.getStoreId()).eq("a.delete_flag", 0);
            StoreInfoVo storeInfoVoOne = storeInfoMapper.getStoreInfoVoOne(queryWrapper);
            map.put("id", taocan.getId());
            map.put("storeName", storeInfoVoOne.getStoreName());
            map.put("phoneNum", storeInfoVoOne.getStorePhone());
            map.put("tijiaoTime", taocan.getCreatedTime());
            String statusStr;
            if (taocan.getStatus() == -1) {
                statusStr = "待审核";
            } else if (taocan.getStatus() == -2) {
                statusStr = "已驳回";
            } else {
                statusStr = "已通过";
            }
            map.put("status", statusStr);
            map.put("taocanName", taocan.getName());
            mapList.add(map);
        }
        returnMap.put("data", mapList);
        returnMap.put("total", totalCnt);
        returnMap.put("current", quanPage.getCurrent());
        returnMap.put("size", quanPage.getSize());
        return returnMap;
    }

    @GetMapping("getTaocanDetial")
    private LifeCoupon getTaocanList(@RequestParam("id") String id) {
        log.info("LifeCouponController.getTaocanList?id={}", id);
        return lifeCouponMapper.selectById(id);
    }

    @GetMapping("updateTaocanStatus")
    private int updateTaocanStatus(@RequestParam("status") String status,
                                   @RequestParam("id") String id,
                                   @RequestParam("approvalComments") String approvalComments) {
        log.info("LifeCouponController.updateTaocanStatus?status={}&id={}&approvalComments={}", status, id, approvalComments);
        LambdaUpdateWrapper<LifeCoupon> wrapper = new LambdaUpdateWrapper();
        Date now = new Date();
        int ret = 0;
        if (status.equals("0")) {
            wrapper.set(LifeCoupon::getStatus, status)
                    .set(LifeCoupon::getApprovalComments, approvalComments)
                    .set(LifeCoupon::getUpdatedTime, now)
                    .eq(LifeCoupon::getId, id);
            ret = lifeCouponMapper.update(null, wrapper);
            return ret;
        } else if (status.equals("-2")) {
            wrapper.set(LifeCoupon::getStatus, status)
                    .set(LifeCoupon::getApprovalComments, approvalComments)
                    .set(LifeCoupon::getUpdatedTime, now)
                    .eq(LifeCoupon::getId, id);
            ret = lifeCouponMapper.update(null, wrapper);
        }
        return ret;
    }
}
