package cn.pluss.platform.controller.officialActivity;


import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.dict.DictService;
import cn.pluss.platform.entity.AppVersionInfo;
import cn.pluss.platform.entity.Dict;
import cn.pluss.platform.entity.OfficialActivity;
import cn.pluss.platform.entity.UserRole;
import cn.pluss.platform.officialActivity.OfficialActivityService;
import cn.pluss.platform.user.UserRoleService;
import cn.pluss.platform.util.OssOperatUtil;
import cn.pluss.platform.util.StringUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 官方活动表 前端控制器
 * </p>
 *
 * @author Djh
 * @since 2020-09-24
 */
@Controller
@RequiredArgsConstructor
@RequestMapping("/officialActivity")
public class OfficialActivityController extends BaseModelController<OfficialActivityService, OfficialActivity> {

    private final OfficialActivityService officialActivityService;

    private final DictService dictService;

    @Resource
    private UserRoleService userRoleService;

    @GetMapping("index")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView("officialActivity/list");
        List<String> list = new ArrayList<String>(){{this.add("SERVICE_PROVIDER_TYPE");}};
        List<Dict> dictList = dictService.getByCode(list);
        view.addObject("dictList", JSONArray.toJSONString(dictList));

        List<UserRole> roleList = userRoleService.list();
        view.addObject("roleList", JSONArray.toJSONString(roleList));

        List<String> actList = new ArrayList<String>(){{this.add("ACTIVITY_TYPE");}};
        List<Dict> actDictList = dictService.getByCode(actList);
        view.addObject("actTypeList", JSONArray.toJSONString(actDictList));

        return view;
    }

    @RequestMapping("add")
    public ModelAndView add() {
        ModelAndView view = new ModelAndView("officialActivity/detail");
        List<String> list = new ArrayList<String>(){{this.add("SERVICE_PROVIDER_TYPE");}};
        List<String> actType = new ArrayList<String>(){{this.add("ACTIVITY_TYPE");}};
        List<UserRole> roleList = userRoleService.list();
        List<Dict> dictList = dictService.getByCode(list);
        List<Dict> actTypeList = dictService.getByCode(actType);
        Map<String, String> uploadParam = OssOperatUtil.getUploadParam();
        view.addObject("uploadParam",uploadParam);
        view.addObject("dictList",dictList);
        view.addObject("actTypeList",actTypeList);
        view.addObject("roleList",roleList);
        return view;
    }

    @Override
    public Result<Page<OfficialActivity>> page(Page<OfficialActivity> page, OfficialActivity condition, OrderItem orderItem) {
        orderItem.setAsc(false).setColumn("createTime");
        return super.page(page, condition, orderItem);
    }

    @GetMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable Integer id){
        ModelAndView view = new ModelAndView("officialActivity/detail");
        List<String> list = new ArrayList<String>(){{this.add("SERVICE_PROVIDER_TYPE");}};
        List<Dict> dictList = dictService.getByCode(list);

        List<String> actType = new ArrayList<String>(){{this.add("ACTIVITY_TYPE");}};
        List<Dict> actTypeList = dictService.getByCode(actType);

        List<UserRole> roleList = userRoleService.list();

        OfficialActivity v = officialActivityService.getById(id);
        Map<String, String> uploadParam = OssOperatUtil.getUploadParam();
        view.addObject("uploadParam",uploadParam);
        view.addObject("officialActivity",v);
        view.addObject("dictList",dictList);
        view.addObject("actTypeList",actTypeList);

        view.addObject("roleList",roleList);
        return view;
    }

    @PostMapping("saveOrUpdate")
    @ResponseBody
    public Result<String> saveOrUpdate(@RequestBody OfficialActivity officialActivity) {
        if (officialActivity.getId() != null){
            officialActivity.setUpdateTime(new Date());
            officialActivity.setActivityGroupNo(officialActivity.getValue());
            officialActivityService.updateById(officialActivity);
        }else {
            if (officialActivity.getActivityGroupNo() != null ){
                String[] split = officialActivity.getActivityGroupNo().split(",");
                for (int i=0;i<split.length;i++){
                    officialActivity.setCreateTime(new Date());
                    officialActivity.setActivityGroupNo(split[i]);
                    officialActivityService.save(officialActivity);
                }
            }
        }
        return ResultGenerator.genSuccessResult("保存成功");
    }

    @PostMapping("delete")
    @ResponseBody
    public Result<String> delete(@RequestBody AppVersionInfo appVersionInfo) {
        officialActivityService.removeById(appVersionInfo.getId());
        if(StringUtil.isNotEmpty(appVersionInfo.getUrl())){
            OssOperatUtil.deleteFile(appVersionInfo.getUrl());
        }
        return ResultGenerator.genSuccessResult("操作成功!");
    }

    @PostMapping("batchDelete")
    @ResponseBody
    public Result<String> batchDelete(@RequestBody List<Integer> ids) {
        officialActivityService.removeByIds(ids);
        return ResultGenerator.genSuccessResult("操作成功!");
    }

}
