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

import java.util.Collection;
import java.util.List;

import cn.com.ty.lift.ud.common.DelReq;
import cn.com.ty.lift.ud.shopping.controller.query.ShoppingQuery;
import cn.com.ty.lift.ud.userCollect.service.IUserCollectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.mysql.cj.util.StringUtils;

import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.dataBank.mapper.entity.LiftBrandEntity;
import cn.com.ty.lift.ud.dataBank.service.ILiftBrandService;
import cn.com.ty.lift.ud.schoolVideo.mapper.entity.SchoolVideoEntity;
import cn.com.ty.lift.ud.shopping.mapper.entity.ShoppingEntity;
import cn.com.ty.lift.ud.shopping.mapper.entity.ShoppingOperateEntity;
import cn.com.ty.lift.ud.shopping.service.IShoppingOperateService;
import cn.com.ty.lift.ud.shopping.service.IShoppingService;
import cn.com.xwy.boot.web.dto.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 商城 API
 * 
 * @author Xson
 * @date 2020-04-23
 */
@Api(tags = "商城 API- 魏瑞锋")
@RestController
@RequestMapping("/api/shoppings")
public class ShoppingController extends BaseController<ShoppingEntity, ShoppingQuery> {

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

    private @Autowired IShoppingService shoppingService;
    private @Autowired IShoppingOperateService shoppingOperateService;
    private @Autowired ILiftBrandService liftBrandService;

    private @Autowired IUserCollectService iUserCollectService;

    @Override
    protected IService<ShoppingEntity> getService() {
        return shoppingService;
    }

    // 新建
    @PostMapping("/insert")
    @ApiOperation(value = "新增")
    public RestResponse<ShoppingEntity> create(@RequestBody ShoppingEntity entity) {
        entity.setBrowseNum(0);
        entity.setFavoriteNum(0);
        entity.setLikeNum(0);
        entity.setHotFlag(0);
        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) {
        shoppingService.updateByIds(ids.getIds(), 1);
        return RestResponse.success("上架成功");
    }

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

    @ApiOperation(value = "批量首页展示")
    @PostMapping("showBath")
    public RestResponse showBath(@RequestBody DelReq ids) {
        shoppingService.updateHotFlagByIds(ids.getIds(), 1);
        return RestResponse.success("批量首页展示");
    }

    @ApiOperation(value = "批量取消首页展示")
    @PostMapping("noshowBath")
    public RestResponse noshowBath(@RequestBody DelReq ids) {
        shoppingService.updateHotFlagByIds(ids.getIds(), 0);
        return RestResponse.success("批量取消首页展示");
    }

    @GetMapping("/detailWithRecord")
    @ApiOperation(value = "获取详情，app端")
    public RestResponse detailWithRecord(Long id, Long userId) {
        ShoppingEntity entity = shoppingService.getById(id);
        shoppingService.updateBrowseNum(id);
        ShoppingOperateEntity findByUserAndId2 = shoppingOperateService.findByUserAndId(userId, id, 3);
        LiftBrandEntity byId = liftBrandService.getById(entity.getBrandId());
        if (byId != null) {
            entity.setBrandName(byId.getName());
        }
        if (findByUserAndId2 != null) {
            entity.setFavoriteId(findByUserAndId2.getId());
            entity.setIsFavorite(1);
        } else {
            entity.setIsFavorite(0);
        }
        return RestResponse.success(entity, "Queried successfully");
    }

    // 根据id查详情
    @GetMapping("/query/{id}")
    @ApiOperation(value = "获取详情")
    @Override
    public RestResponse get(@PathVariable Long id) {
        ShoppingEntity entity = getService().getById(id);
        LiftBrandEntity byId = liftBrandService.getById(entity.getBrandId());
        if (byId != null) {
            entity.setBrandName(byId.getName());
        }
        return RestResponse.success(entity, "Queried successfully");
    }

    // 根据条件查询
    @PostMapping("/query-page")
    @ApiOperation(value = "分页查询")
    @Override
    public RestResponse getCondition(@RequestBody ShoppingQuery request) {
        if (StringUtils.isNullOrEmpty(request.getPlatform())) {
            return RestResponse.fail("平台不能为空");
        }
        IPage<ShoppingEntity> list = getService().page(request.page(), request.condition());
        for (ShoppingEntity se : list.getRecords()) {
            LiftBrandEntity byId = liftBrandService.getById(se.getBrandId());
            if (byId != null) {
                se.setBrandName(byId.getName());
            }
        }
        return RestResponse.success(list, "Queried successfully");
    }

    /**
     * 重写根据id批量删除的方法
     * 
     * @param ids
     * @return
     */
    @DeleteMapping("/delete-batch")
    @ApiOperation(value = "批量删除")
    @Override
    public RestResponse deleteBath(@RequestBody DelReq req) {
        boolean deleteFlag = getService().removeByIds(req.getIds());
        if (deleteFlag) {
            for (Long id : req.getIds()) {
                // 删除 user_collect 我的收藏的内容
                iUserCollectService.deleteByTypeAndDataId(id, 4);
            }
            return RestResponse.success(null, "删除成功");
        } else {
            return RestResponse.fail("删除失败");
        }
    }

    /**
     * 重写根据id删除的方法
     * 
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "根据id删除")
    @Override
    public RestResponse delete(@RequestBody DelReq req) {
        boolean deleteFlag = getService().removeById(req.getId());
        if (deleteFlag) {
            // 删除 user_collect 我的收藏的内容
            iUserCollectService.deleteByTypeAndDataId(req.getId(), 4);
            return RestResponse.success(null, "删除成功");
        } else {
            return RestResponse.fail("删除失败");
        }
    }
}