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

import cn.com.ty.lift.ud.common.DelReq;
import cn.com.ty.lift.ud.shopping.controller.query.ShoppingOperateQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.service.IService;

import cn.com.ty.lift.ud.common.BaseController;
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.ty.lift.ud.userCollect.mapper.entity.UserCollectDto;
import cn.com.ty.lift.ud.userCollect.mapper.entity.UserCollectEntity;
import cn.com.ty.lift.ud.userCollect.service.IUserCollectService;
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/shoppingOperates")
public class ShoppingOperateController extends BaseController<ShoppingOperateEntity, ShoppingOperateQuery> {

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

    private @Autowired IShoppingOperateService shoppingOperateService;
    private @Autowired IUserCollectService iUserCollectService;
    private @Autowired IShoppingService shoppingService;

    @Override
    protected IService<ShoppingOperateEntity> getService() {
        return shoppingOperateService;
    }

    @Override
    @PostMapping("/insert")
    @ApiOperation(value = "新增")
    public RestResponse<ShoppingOperateEntity> create(@RequestBody ShoppingOperateEntity entity) {
        // 保存用户收藏记录
        UserCollectDto userCollect = new UserCollectDto();
        userCollect.setUserId(entity.getUserId());
        userCollect.setCollectType(4);
        userCollect.setInfoId(entity.getDataId());
        UserCollectEntity collect = iUserCollectService.isCollect(userCollect);
        if (null == collect) {
            UserCollectEntity userCollectEntity = new UserCollectEntity();
            BeanUtils.copyProperties(userCollect, userCollectEntity);
            iUserCollectService.save(userCollectEntity);
        }
        ShoppingOperateEntity findByUserAndId =
            shoppingOperateService.findByUserAndId(entity.getUserId(), entity.getDataId(), entity.getType());
        boolean saveFlag = false;
        if (findByUserAndId == null) {
            saveFlag = shoppingOperateService.save(entity);
        }
        ShoppingOperateEntity byId = shoppingOperateService.getById(entity.getId());
        if (saveFlag) {
            if (entity.getType() == 3) {
                ShoppingEntity byId2 = shoppingService.getById(entity.getDataId());
                byId2.setFavoriteNum(byId2.getFavoriteNum() == null ? 1 : byId2.getFavoriteNum() + 1);
                shoppingService.updateById(byId2);
            }
            // if (entity.getType() == 2) {
            // ShoppingEntity byId2 = shoppingService.getById(entity.getDataId());
            // byId2.setLikeNum(byId2.getLikeNum() == null ? 1 : byId2.getLikeNum() + 1);
            // shoppingService.updateById(byId2);
            // }
            return RestResponse.success(null, "Created successfully");
        } else {
            return RestResponse.fail("Created unsuccessfully");
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "根据id删除")
    public RestResponse delete(@RequestBody DelReq req) {
        ShoppingOperateEntity byId2 = shoppingOperateService.getById(req.getId());
        ShoppingEntity byId = shoppingService.getById(byId2.getDataId());
        if (byId2.getType() == 3) {
            byId.setFavoriteNum(byId.getFavoriteNum() == null ? 0 : byId.getFavoriteNum() - 1);
            shoppingService.updateById(byId);
        }
        iUserCollectService.deleteByUserAndDataId(byId2.getUserId(), byId2.getDataId(), 4);
        boolean deleteFlag = getService().removeById(req.getId());
        if (deleteFlag) {
            return RestResponse.success(null, "Deleted successfully");
        } else {
            return RestResponse.fail("Deleted unsuccessfully");
        }
    }
}