package cn.com.ctop.kuaishou.modules.live.controller;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.live.entity.LiveDataAccounts;
import cn.com.ctop.kuaishou.modules.live.service.ILiveDataAccountsService;
import cn.com.ctop.kuaishou.modules.live.service.ILiveDataImportRecordService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 直播数据——操作记录表
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2022-04-01
 */
@Slf4j
@Api(tags = "直播数据——操作记录表")
@RestController
@RequestMapping("/liveData/accounts")
public class LiveDataAccountsController {
    @Autowired
    private ILiveDataAccountsService liveDataAccountsService;
    @Autowired
    private ILiveDataImportRecordService recordService;

    /**
     * 分页列表查询
     */
    @GetMapping(value = "/list")
    public Result<IPage<LiveDataAccounts>> queryPageList(LiveDataAccounts liveDataAccounts,
                                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                         HttpServletRequest req) {
        Result<IPage<LiveDataAccounts>> result = new Result<>();
        QueryWrapper<LiveDataAccounts> queryWrapper = QueryGenerator.initQueryWrapper(new LiveDataAccounts(), req.getParameterMap());
        Page<LiveDataAccounts> page = new Page<LiveDataAccounts>(pageNo, pageSize);
        if (!Check.isNull(liveDataAccounts.getLiveAccountId())) {
            queryWrapper.like("live_account_id", liveDataAccounts.getLiveAccountId());
        }
        if (!Check.isNull(liveDataAccounts.getLiveAccountName())) {
            queryWrapper.like("live_account_name", liveDataAccounts.getLiveAccountName());
        }
        if (!Check.isNull(liveDataAccounts.getLiveAccountType())) {
            queryWrapper.eq("live_account_type", liveDataAccounts.getLiveAccountType());
        }
        if (!Check.isNull(liveDataAccounts.getProductName())) {
            queryWrapper.like("product_name", liveDataAccounts.getProductName());
        }
        if (Check.isNull(liveDataAccounts.getState())) {
            queryWrapper.eq("state", 1);
        } else {
            queryWrapper.eq("state", liveDataAccounts.getState());
        }
        queryWrapper.orderByDesc("create_time");
        IPage<LiveDataAccounts> pageList = liveDataAccountsService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 分页列表查询
     */
    @GetMapping(value = "/typeList")
    public Result<Object> typeList() {
        try {
            List<JSONObject> list = liveDataAccountsService.typeList();
            return Result.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("查询异常");
    }

    /**
     * 添加直播账户
     */
    @PostMapping(value = "/add")
    public Result<Object> add(@RequestBody LiveDataAccounts liveDataAccounts) {
        try {
            if (Check.isNull(liveDataAccounts.getLiveAccountId())) {
                return Result.error("缺少数据，请输入账户ID");
            } else if (Check.isNull(liveDataAccounts.getLiveAccountName())) {
                return Result.error("缺少数据，请输入账户名称");
            }
            LiveDataAccounts accounts = liveDataAccountsService.getById(liveDataAccounts.getLiveAccountId());
            if (!Check.isNull(accounts)) {
                return Result.error("该账户ID已注册，账户名：" + accounts.getLiveAccountName());
            }
            QueryWrapper<LiveDataAccounts> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("live_account_name", liveDataAccounts.getLiveAccountName());
            queryWrapper.eq("state", "1").last("limit 1");
            LiveDataAccounts one = liveDataAccountsService.getOne(queryWrapper);
            if (!Check.isNull(one)) {
                return Result.error("该账户名称已存在，请重新命名");
            }
            liveDataAccounts.setStatDate(DateUtils.date2Str());
            liveDataAccountsService.save(liveDataAccounts);
            recordService.addImportRecord(liveDataAccounts.getLiveAccountId(), 101, liveDataAccounts.getUserId());
            return Result.ok("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("操作失败");
        }
    }

    /**
     * 修改直播账户
     */
    @PostMapping(value = "/edit")
    public Result<Object> edit(@RequestBody LiveDataAccounts liveDataAccounts) {
        if (Check.isNull(liveDataAccounts.getLiveAccountId())) {
            return Result.error("缺少数据，请输入账户ID");
        }
        LiveDataAccounts accounts = liveDataAccountsService.getById(liveDataAccounts.getLiveAccountId());
        if (Check.isNull(accounts)) {
            return Result.error("修改失败，未找到该账户");
        }
        if (!Check.isNull(liveDataAccounts.getLiveAccountName())) {
            QueryWrapper<LiveDataAccounts> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("live_account_name", liveDataAccounts.getLiveAccountName());
            queryWrapper.eq("state", 1).last("limit 1");
            LiveDataAccounts one = liveDataAccountsService.getOne(queryWrapper);
            if (!Check.isNull(one) && one.getLiveAccountId() - liveDataAccounts.getLiveAccountId() != 0) {
                return Result.error("修改失败，该账户名称已存在，请重新命名");
            }
        }
        liveDataAccountsService.updateById(liveDataAccounts);
        recordService.addImportRecord(liveDataAccounts.getLiveAccountId(), 103, liveDataAccounts.getUserId());
        return Result.ok("修改成功!");
    }

    /**
     * 通过id删除
     */
    @GetMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "liveAccountId") String liveAccountId, String userId) {
        try {
            if (Check.isNull(userId) || Check.isNull(liveAccountId)) {
                return Result.error("缺少数据");
            }
            LiveDataAccounts accounts = liveDataAccountsService.getById(liveAccountId);
            if (!Check.isNull(accounts)) {
                accounts.setState("0");
                liveDataAccountsService.updateById(accounts);
                //删除账户，其他报表的数据状态设为无效 0
                liveDataAccountsService.updateOtherReport(liveAccountId);
                recordService.addImportRecord(accounts.getLiveAccountId(), 102, userId);
            }
        } catch (Exception e) {
            log.error("删除失败", e.getMessage());
            return Result.error("删除失败!");
        }
        return Result.ok("删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "直播数据——操作记录表-通过id查询", notes = "直播数据——操作记录表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<LiveDataAccounts> queryById(@RequestParam(name = "id", required = true) String id) {
        Result<LiveDataAccounts> result = new Result<>();
        LiveDataAccounts liveDataAccounts = liveDataAccountsService.getById(id);
        if (liveDataAccounts == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(liveDataAccounts);
            result.setSuccess(true);
        }
        return result;
    }

}
