package cn.pluss.platform.controller.officialGoods.exchange;


import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.device.DeviceBsjService;
import cn.pluss.platform.entity.DeviceBsj;
import cn.pluss.platform.entity.OfficialGoodsExchange;
import cn.pluss.platform.officialGoods.exchange.OfficialGoodsExchangeService;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Djh
 * @since 2020-10-09
 */
@Controller
@RequestMapping("/officialGoodsExchange")
public class OfficialGoodsExchangeController extends BaseModelController<OfficialGoodsExchangeService, OfficialGoodsExchange> {

    @Autowired
    private DeviceBsjService bsjService;

    @GetMapping("index")
    public String index() {
        return "officialGoods/exchange/list";
    }

    @Override
    public Result<Page<OfficialGoodsExchange>> page(Page<OfficialGoodsExchange> page, OfficialGoodsExchange officialGoodsExchange,OrderItem orderItem) {
        QueryWrapper<OfficialGoodsExchange> queryWrapper = new QueryWrapper<OfficialGoodsExchange>().orderByDesc("createTime");
        if(StringUtil.isNotEmpty(officialGoodsExchange.getUserName())){
            queryWrapper.like("userName",officialGoodsExchange.getUserName());
        }
        if(StringUtil.isNotEmpty(officialGoodsExchange.getUserId())){
            queryWrapper.eq("userId",officialGoodsExchange.getUserId());
        }
        if(StringUtil.isNotEmpty(officialGoodsExchange.getPhone())){
            queryWrapper.eq("phone",officialGoodsExchange.getPhone());
        }
        if(officialGoodsExchange.getStatus() != null){
            queryWrapper.eq("status",officialGoodsExchange.getStatus());
        }
        if(officialGoodsExchange.getDeliveryStatus() != null){
            queryWrapper.eq("deliveryStatus",officialGoodsExchange.getDeliveryStatus());
        }
        if(officialGoodsExchange.getName() != null){
            queryWrapper.like("name",officialGoodsExchange.getName());
        }
        page = this.baseService.page(page, queryWrapper);
        return ResultGenerator.genSuccessResult(page);
    }

    @Override
    public Result<String> save(@RequestBody OfficialGoodsExchange entity) {
        entity.setDeliveryTime(new Date());

        final OfficialGoodsExchange existData = baseService.getById(entity.getId());
        if ("移动4G流量年卡流量".equals(entity.getName())) {
            // 充值流量的订单，自动点击发货后，即表示已充值新的一年的流量，将对应机具的流量到期时间往后加一年
            LambdaQueryWrapper<DeviceBsj> qWrapper = Wrappers.lambdaQuery();
            qWrapper.eq(DeviceBsj::getDeviceNo, existData.getAddress())
                    .or()
                    .eq(DeviceBsj::getSnNo, existData.getAddress());

            final DeviceBsj one = bsjService.getOne(qWrapper);
            if (one != null) {
                final Date expireTime = one.getSimExpireTime();
                if (expireTime != null) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(expireTime);
                    calendar.add(Calendar.YEAR, 1);
                    one.setSimExpireTime(calendar.getTime());
                    bsjService.updateById(one);
                }
            }
        }
        return super.save(entity);
    }
}
