package com.platform.yijia.controller;

import com.google.gson.Gson;
import com.platform.yijia.config.YiJiaRequest;
import com.platform.yijia.pojo.CustomerLabel;
import com.platform.yijia.service.LabelService;
import com.platform.yijia.utils.CodeMsg;
import com.platform.yijia.utils.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/*
 * <Title> LabelController </Title>
 * <Description> 标签模块 </Description>
 * @Author JK
 * @Date 2021年7月15日
 */
@Controller
@YiJiaRequest()
@CrossOrigin
@Api(tags = "标签")
public class LabelController {
    private static Logger logger =(Logger) LoggerFactory.getLogger(LabelController.class);

    @Resource
    private LabelService labelService;

    //用户扫码进标签
    @RequestMapping(value = "/addUserToLabel", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "用户扫码进标签", httpMethod = "GET", notes = "用户扫码进标签")
    public String addUserToLabel(@ApiParam(value = "油站ID", required = true) @RequestParam Integer stationId,
                                 @ApiParam(value = "手机号", required = true) @RequestParam String phone,
                                 @ApiParam(value = "标签ID", required = true) @RequestParam String labelId,
                                 @ApiParam(value = "创建人", required = true) @RequestParam String createBy){
        Gson gson =new Gson();
        ResultData resultData = null;       //返回结果集
        try {
            CustomerLabel customerLabel = new CustomerLabel();
            customerLabel.setStationId(stationId);
            customerLabel.setPhone(phone);
            //获取该标签用户信息
            CustomerLabel c = labelService.getCustomerLabel(customerLabel);
            if(c !=null){
                customerLabel.setLabelId(labelId);
                customerLabel.setUpdateBy(createBy);
                customerLabel.setUpdateTime(new Date());
                labelService.updateCustomerLabel(customerLabel);
            }else {
                customerLabel.setCreateBy(createBy);
                customerLabel.setCreateTime(new Date());
                customerLabel.setDelFlag("0");
                customerLabel.setLabelId(labelId);
                labelService.insertCustomerLabel(customerLabel);
            }
            resultData = ResultData.success(CodeMsg.SUCCESS);
        }catch (Exception e){
            logger.info("用户扫码进标签错误信息: "+  e.getMessage());
            resultData = ResultData.error(CodeMsg.YEWU_FAIL);
        }
        return gson.toJson(resultData);
    }
}
